0

I'm trying to print my card widget title in the card details page but I'm getting " A non-null String must be provided to a Text widget".

Any suggestion or help on how can I fix this?.

Model.dart

class Item {
  int id;
  String title;

  Item(
      {this.id, this.title });
}

CardWidget.dart

import 'package:maxis_mobile/ui/screens/cardDetails-screen.dart';
import 'cardModel.dart';

class _CardWidgetState extends State<CardWidget> {
  final item = Item();
  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: Card(
          child: InkWell(
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => CardDetails(item: item), //not sure this is right.
                  ));
            },
            child: Column(children: [
              Container(
                child: Row(
                  children: [
                    Container(
                      child: Text(
                        widget.title,  // Card Title
                        ),
                      ),
                    ),

CardDetails.dart

import 'package:flutter/material.dart';
import '../shared/cardModel.dart';

class CardDetails extends StatelessWidget {
  final Item item;

  CardDetails({Key key, @required this.item}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(item.title),
    );
  }
}

DummyData.dart

    List<Item> items = [
      Item(id: 0, title: '1. US Daily Retail Delivery By Brands'),
]
  • Where did you use the dummy data in `_CardWidgetState`? You created an Item variable in `_CardWidgetState` without specifying the title, that is what you are passing to the `CardDetails` screen. – Josteve Nov 24 '20 at 19:39

2 Answers2

1

In _CardWidgetState you have defined an empty object. That object you passed to the CardDetails page through the constructor, and on the CardDetails page you try in Text widget call item.title but it is null (without value). You need populate this object or try with hardcode string.

Same ase here: A non-null String must be provided to a Text widget

Boris Ilic
  • 11
  • 2
  • Hi Boris, I'm sorry I forgot to mention I have dummy data that feeding all the title for the cardWidget so can you please take a look at my code again. thanks –  Nov 24 '20 at 18:23
  • You have 2 options: 1. list of item replace in _CardWidgetState and pass in constructor CardDetails(item: items[0]), 2. in _CardWidgetState you need to initialize your item final item = Item(id: 1, title: "first item"); – Boris Ilic Nov 26 '20 at 08:14
0

The cause is item declared in CardWidget.dart - there is no title, so item.title in CardDetails.dart is null. To fix the error you can add default value for tile field in Item class:

class Item {
  int id;
  String title;

  Item({this.id, this.title = ''}) : assert(title != null);
}
Owczar
  • 2,335
  • 1
  • 17
  • 24
  • Hi Owczar, I have dummyData.dart file that feeding all the dummy title so can you please take a look at my code again please. –  Nov 24 '20 at 18:22
  • Yes, but still: you have the `item` final field in `_CardWidgetState` class. Field initialized in that way always has an object of class `Item` with empty data (because of the line: `final item = Item();`). You can move this field to `CardWidget` and access it by `widget.item` inside the `_CardWidgetState` class. – Owczar Nov 24 '20 at 18:58