1 year ago

#364821

test-img

Mikkel Ontberg

FutureBuilder has empty snapshot despite Future returns proper data

I have FutureBuilder widget as Scaffold's body and I want to display a list of widgets when database returns required info but while database is properly returning data the FutureBuilder does not show any widgets(like CircularProgressIndicator or required List as it finishes its job). Code shown below:

Future<List<Widget>> getList() async{
  List<Widget> list = [];
  var db = dbMethods();
  db.getConnection().then((conn) async {
    await Future.delayed(const Duration(seconds: 1));
    var result = await conn.query("select * from info", []);

    for(var row in result) {
      var item = BilbordItem(
          firm: row[2],
          constructionType: row[3],
          demonstrationType: row[4],
          lat: double.parse(row[5]),
          long: double.parse(row[6]),
          sizeX: double.parse(row[7]),
          sizeY: double.parse(row[8]),
          address: row[9],
          positionRate: int.parse(row[10]),
          colorsRate: int.parse(row[11]),
          passabilityRate: int.parse(row[12]),
          typeRate: int.parse(row[13])
      );
      print(item.address);  //here i was checking if database fetch is working and it does print required info
      list.add(item);
    }
    conn.close();
  });
  return list;
}

class _ListPage extends State<ListPage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Список билбордов"),
        centerTitle: true,
      ),
      body: FutureBuilder(
            future: getList(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
                if(snapshot.connectionState == ConnectionState.done){
                  if(snapshot.hasData && !snapshot.hasError){
                    return Center(
                      child: Column(
                        children: snapshot.data,
                      ),
                    );
                  }
                  else if(snapshot.hasError){
                    return SingleChildScrollView(
                      child: Center(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            const Icon(Icons.error_outline, color: Colors.red,),
                            Text(snapshot.error.toString()),
                          ],
                        ),
                      ),
                    );
                  }
                }else {
                  return Center(child: CircularProgressIndicator());
                }
                return Center(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: const [
                      Text("Empty"),
                    ],
                  ),
                );
            },
          ),
    );
  }
}

flutter

dart

flutter-futurebuilder

0 Answers

Your Answer

Accepted video resources