Home > Blockchain >  Bad state: field does not exist within the DocumentSnapshotPlatform even though there is a field �
Bad state: field does not exist within the DocumentSnapshotPlatform even though there is a field �

Time:06-05

Im tryig to implement search page for the products in firestore. But when im trying to list products' names in upper case, it gives an error

The following StateError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#059ea): Bad state: field does not exist within the DocumentSnapshotPlatform"

if i try to list products' names in lower case, it does not throw an error but also it does not work.

Note: There is a field 'name' in firestore, so i did not understand what the problem is.

Here is my code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class SearchScreen extends StatefulWidget {
  const SearchScreen({Key? key}) : super(key: key);

  @override
  State<SearchScreen> createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen> {
  String searchInput = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade300,
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.grey.shade300,
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios_new,
            color: Colors.black,
          ),
          onPressed: (){
            Navigator.pop(context);
          },
        ),
        title: CupertinoSearchTextField(
          autofocus: true,
          backgroundColor: Colors.white,
          onChanged: (value){
            setState((){
              searchInput = value;
            });
          },
        ),
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection('products').snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
          if(snapshot.connectionState == ConnectionState.waiting){
            return const Material(
              child: Center(
                child: CircularProgressIndicator(),
              ),
            );
          }
          final result = snapshot.data!.docs.where(
                  (e) => e['name'.toLowerCase()]
                      .contains(searchInput.toLowerCase()),
          );
          return ListView(
            children: result.map((e) => Text(e['name'.toUpperCase()])).toList(),
          );
        },
      ),
    );
  }
}

What is the problem?

CodePudding user response:

I get the answer. Firestore does not support toLowerCase(), toUpperCase(). Therefore, it does not work.

CodePudding user response:

Try this:

e.get('name').toString().toLowerCase();

That should work.

CodePudding user response:

Try this:

final result = snapshot.data!.docs.where(
              (e) => e['name'].toLowerCase()
                  .contains(searchInput.toLowerCase()),);
result.map((e) => Text(e['name'].toUpperCase())).toList(),
  • Related