Akash Patil
Akash Patil

Reputation: 1

rangeerror (index): invalid value: valid value range is empty: 0 in flutter while getting data from firebase

body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            StreamBuilder(
              stream: Firestore.instance.collection('scores').snapshots(),
              builder: (context, snapshot)
              {
                if(snapshot.hasData) return Text('Loading Data...');
                return Column(
                  children: <Widget>[
                    Text(snapshot.data.documents[0]['TeamOne']),
                    Text(snapshot.data.documents[0]['TeamTwo']),
                    Text(snapshot.data.documents[0]['toWin'].toString()),
                    Text(snapshot.data.documents[0]['runs'].toString()),
                    Text(snapshot.data.documents[0]['wickets'].toString()),
                    Text(snapshot.data.documents[0]['overs'].toString()),
                  ],
                );
              },

            )
          ],
        ),
      ),

I'm trying to get data from firebase but getting this error. first it goes to the first return statement and after some time it shows the red screen with error "rangeerror (index): invalid value: valid value range is empty: 0"

Upvotes: 0

Views: 388

Answers (1)

Yuu Woods
Yuu Woods

Reputation: 1348

I think you can use "!".

if(!snapshot.hasData) return Text('Loading Data...');

Upvotes: 1

Related Questions