Streambuilder – error after flutter migration – snapshot syntax

Issue

I used this code bevor migration

StreamBuilder(
          stream: FirebaseDatabase.instance
              .ref()
              .child('user')
              .child(_userID)
              .onValue,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              _networkImageUrl = snapshot.data.snapshot.value["img"];
              return Text(_networkImageUrl.toString());
             } else {
               return Container();
            }
          }),

after "dart pub upgrade –null-safety" I get the error:

The property 'snapshot' can't be unconditionally accessed because the receiver can be 'null'.

I tried to fix it with "!" but it doesn’t work, it keeps the same error

Solution

this is the code how it works:

StreamBuilder<DatabaseEvent>(
          stream: FirebaseDatabase.instance
              .ref()
              .child('user')
              .child(_userID)
              .onValue,
          builder: (BuildContext context,AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              Map<dynamic, dynamic> userDocument = snapshot.data.snapshot.value;
              _networkImageUrl = userDocument["img"]
              return Text(_networkImageUrl.toString());
             } else {
               return Container();
            }
          }),

thx 2 h8moss

Answered By – Bernhard

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *