Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another exception was thrown: FormatException: Invalid number (at character 1)

Why does the error Another exception was thrown: FormatException: Invalid number (at character 1) occur on my screen for a few microseconds before all is back to normal. Sometimes it doesn't even occur. Below is my StreamBuilder function:

_delivered() {
    print('In the delivered function:${resId},${customerId}, ');
    return StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance
            .collection('restaurants')
            .document(resId)
            .collection('customers')
            .document(customer)
            .collection('orders')
            .where('deliveryTime', isGreaterThan: '')
            .snapshots(),
        builder: (context, snapshot) {
          print('Does snapshop have data? ${snapshot.hasData}');
          if (!snapshot.hasData) return Container();

          List deliveredListFromServer = snapshot.data.documents;
          return Expanded(
            child: ListView(
              shrinkWrap: true,
              children: deliveredListFromServer.map((item) {
                print('document id: ${item.documentID}');
                return InkWell(
                  child: SizedBox(
                    height: 50,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(
                          width: 80,
                          child: Text(
                            item['orderBy']['username'],
                            textAlign: TextAlign.center,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ),
                        SizedBox(
                          width: 5,
                        ),
                        Expanded(
                          child: ListView(
                            scrollDirection: Axis.horizontal,
                            children: item['order'].map<Widget>((item) {
                              return SizedBox(
                                width: 80,
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    '${item['qty']} ${item['drinkName']}',
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                              );
                            }).toList(),
                          ), //
                        ),
                        SizedBox(
                          width: 5,
                        ),
                        SizedBox(
                          width: 60,
                          child: Text(DateFormat('h:mm a').format(
                              DateTime.fromMillisecondsSinceEpoch(
                                  int.parse(item['deliveryTime'])))),
                        )
                      ],
                    ),
                  ),
                  onTap: () {
                    _deliveredDetail(item);
                  },
                );
              }).toList(),
            ),
          );
        });
  }

This is my console:

I/flutter (11506): In the delivered function:XufIsxA8a24lLhO6gTr1,zMrQmcoQwci9bVVRo6tx, 
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562
I/flutter (11506): document id: 1579595374166
I/flutter (11506): Another exception was thrown: FormatException: Invalid number (at character 1)
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562

From the console, I don't even understand why it's bringing document id: 1579595374166 from the database. Only document id: 1579534059562 has a deliveryTime set. The database has 6 records, only one has a deliveryTime set. Others are empty "" strings.

So after a few milliseconds, everything works as expected i.e. the proper UI with only one item for the database showing on the screen. It looks like everything is back to normal the second time the stream returns only one document. In fact, the only time it doesn't bring the red screen is when the console looks like this:

I/flutter (11506): In the delivered function:XufIsxA8a24lLhO6gTr1,zMrQmcoQwci9bVVRo6tx, 
I/flutter (11506): Does snapshop have data? false
I/flutter (11506): Does snapshop have data? true
I/flutter (11506): document id: 1579534059562

This also means the streamBuilder is passing incorrect data to the list (and probable source of error). Why is the query returning the wrong results sometimes?!

like image 910
Paul Kitatta Avatar asked Dec 06 '22 08:12

Paul Kitatta


1 Answers

This error occurs when you are fetching a data which is null, I encountered the same issue and was able to solve it by removing that null data from my firestore database.

I would suggest you check the data in the collection from where you are fetching the list, one of the fields must be null there.

like image 78
Sarang Pal Avatar answered May 25 '23 04:05

Sarang Pal