0

i am trying to create a screen which uses future builder and when there is data in the database it works great but when there is no data found its shows error:

FormatException: Unexpected end of input (at character 2)

and here is my code:

 FutureBuilder(
            future: allCars(),
            builder: (context, AsyncSnapshot snapshot){
              if(!snapshot.hasError){
  
                    return Center(child: Text("No Cars"));
                }

              else {
                if (snapshot.hasError) print(snapshot.error);
                return snapshot.hasData ? ListView.builder(
                    itemCount: snapshot.data!.length,
                    itemBuilder: (context, index) {
                      List list = snapshot.data;
                      return SingleChildScrollView()
                    }

it must show : Text("No Cars")) on the screen . but it keeps reloading when there is no data found? Thanks in advance <3 .

2
  • When no data is your Future function returning null?
    – Diwyansh
    Jan 3, 2022 at 11:29
  • I prefer like FutureBuilder<futureDataType?>( and you can just do !snapshot.hasData Jan 3, 2022 at 11:29

1 Answer 1

1

Use if(snapshot.hasData) to check if there is data or not.

    FutureBuilder(
                future: allCars(),
                builder: (context, AsyncSnapshot snapshot){
                  if(!snapshot.error){
      
                        return Center(child: Text("No Cars"));
                    }
    
                  else {
                    if (snapshot.hasdata) {

                    return  ListView.builder(
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          List list = snapshot.data;
                          return SingleChildScrollView()
                        }else{
return Text("No cars")}
1
  • works Perfect thanks @Nbn
    – CodingBee
    Jan 5, 2022 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.