narayan rathod
narayan rathod

Reputation: 81

Flutter StreamProvider returns null object eventhough stream is returning objects

    import 'package:flutter/material.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:provider/provider.dart';
    import 'package:collection/collection.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        var _db = FirestoreService();
        return MultiProvider(
          providers: [
            StreamProvider(create:(context)=>_db.getItems(),
              catchError:(BuildContext context,e){
              print("Error:$e");
              return null;
              },
              updateShouldNotify:const ListEquality<Item>().equals),
          ],

          child: MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: Catalog(),
          ),
        );
      }
    }

    class Item{
      String name;
      double price;

      Item({this.name,this.price});

      Item.fromJSON(Map<String,dynamic> json)
      :name=json['name'],
      price=json['price'];

      factory Item.fromMap(Map data)
      {
        return Item(name: data['name'],price:data['price']);
      }

    }
    class FirestoreService{

      var _db = Firestore.instance;

      Stream<List<Item>> getItems()
      {
        return _db.collection('akurdi')
            .snapshots()
            .map((snapshot)=>snapshot.documents
            .map((document)=>Item.fromMap(document.data)).toList());
      }

    }

    class Catalog extends StatelessWidget {
      @override
      Widget build(BuildContext context) {

        var items = Provider.of<List<Item>>(context);

        print(items.length);

        return ListView.builder(
          itemCount: items.length,
          itemBuilder: (context,index){
              return ListTile(
              title: Text(items[index].name),
              trailing: Text(items[index].price.toString())
              );
          },
        );
      }
    }

Errors:

I/flutter (12444): The getter 'length' was called on null.

I/flutter (12444): Receiver: null

I/flutter (12444): Tried calling: length

When i debug the code specially the getItems() function , I see the actual values from the firestore database but when i use the provider

var items = Provider.of>(context) to get the values,it returns null.

Upvotes: 0

Views: 1104

Answers (1)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

I think updateShouldNotify is the real problem here. until that condition is not true, data will not change.

Just simply remove that updateShouldNotify and it will work.

You can use it when you want to control when should data has to be change. for example any new data added or remove then you can compare previous and current data length and update accordingly.

Upvotes: 2

Related Questions