0

App is working smoothly. But Error is showing on the console.

The getter 'length' was called on null. Receiver: null Tried calling: length User-created ancestor of the error-causing widget was:
StreamProvider> file:///Users/Documents/flutter_job_manager/lib/components/router.dart:23:24

I use Firebase Cloud Storage with Provider

Error is coming here:(on my generateRoute)

  case AddCategory.route:
    return MaterialPageRoute(
      builder: (_) => StreamProvider<List<CategoryModel>>.value(
        value: DatabaseService().streamCategories(),
          child:  AddCategory()
      )
    );

just to know: streamCategories in DatabaseService()

  Stream<List<CategoryModel>> streamCategories() {
    var ref = _db.collection('categories');

    return ref.snapshots().map((list) =>
        list.documents.map((doc) => CategoryModel.fromFirestore(doc)).toList());
  }

AddCategory in StatelessWidget

      StreamProvider<List<CategoryModel>>.value(
          value: db.streamCategories(),
          child:ListView.builder(
                itemCount: category.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                      title: Text(
                        category[index].categoryName,
                        ),
                  );
                }),
          ))
BIS Tech
  • 17,000
  • 12
  • 99
  • 148

1 Answers1

1

I added initialData. Now no errors on console..

  return MaterialPageRoute(
      builder: (_) => StreamProvider<List<CategoryModel>>.value(
        initialData: [CategoryModel(categoryName: "no current categories")],
        value: DatabaseService().streamCategories(),
          child:  AddCategory()
      )
    );
BIS Tech
  • 17,000
  • 12
  • 99
  • 148