Home > Software engineering >  flutter error: Too many positional arguments: 0 expected, but 1 found in streambuilder
flutter error: Too many positional arguments: 0 expected, but 1 found in streambuilder

Time:07-02

flutter error: Too many positional arguments: 0 expected, but 1 found in streambuilder i have seen many article releted this but i can't get proper solution so please help me how to resolve it.

Code:

 StreamBuilder(
 stream: _products.snapshots(), //build connection
 builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
 if (streamSnapshot.hasData) {
 return ListView.builder(
 itemCount: streamSnapshot.data!.docs.length, //number of rows
 itemBuilder: (context, index) {
 final DocumentSnapshot documentSnapshot =
 streamSnapshot.data!.docs[index];
 return Card(
 margin: const EdgeInsets.all(10),
 child: ListTile(
 title: Text(documentSnapshot['name']),
 subtitle: Text(documentSnapshot['price'].toString()),
                  ),
                );
              },
            );
          }

 return const Center(
 child: CircularProgressIndicator(),
          );
        },
      )

enter image description here

CodePudding user response:

Your stream builder looks like it's not in the widget tree. Please try this

Scaffold(
floatingActionButton : FloatingActionButton(),
body : SizedBox(
        width: double.infinity,
        child: Center(
          child: StreamBuilder(
            stream: _products.snapshots(),
            initialData: 0,
            builder: (
                BuildContext context,
                AsyncSnapshot<QuerySnapshot> snapshot,
                ) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Container( );
              } else if (snapshot.connectionState == ConnectionState.active
                  || snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return const Text('Error');
                } else if (snapshot.hasData) {
                  return Text(
                      snapshot.data.toString(),
                  );
                } else {
                  return const Text('Empty data');
                }
              } else {
                return Text('State: ${snapshot.connectionState}');
              }
),
  • Related