Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created March 3, 2018 21:32
Show Gist options
  • Save branflake2267/e55a5e0650157f6ff41329ff4c0ea3ce to your computer and use it in GitHub Desktop.
Save branflake2267/e55a5e0650157f6ff41329ff4c0ea3ce to your computer and use it in GitHub Desktop.
Flutter - Using the future builder with infinite list view.
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
var futureBuilder = new FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Text('loading...');
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return createListView(context, snapshot);
}
},
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Home Page"),
),
body: futureBuilder,
);
}
Future<List<String>> _getData() async {
var values = new List<String>();
values.add("Horses");
values.add("Goats");
values.add("Chickens");
//throw new Exception("Danger Will Robinson!!!");
await new Future.delayed(new Duration(seconds: 5));
return values;
}
Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
List<String> values = snapshot.data;
return new ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
new ListTile(
title: new Text(values[index]),
),
new Divider(height: 2.0,),
],
);
},
);
}
}
@branflake2267
Copy link
Author

branflake2267 commented Mar 3, 2018

@branflake2267
Copy link
Author

What the app does above:

screen shot 2018-03-03 at 11 14 37 am

@alvinkonda
Copy link

A video for the AnimatedList would be great. Thnx

@jairwen
Copy link

jairwen commented May 3, 2018

Great Job!

  • How to make it real infinite view?
  • how to trigger next data loading by scroll or by index condition?
  • how to refresh by timer loading different data?
  • how to setState() after added data or modified data, will it rebuild and rerender whole showing ListView?
    thanks

@alfianyusufabdullah
Copy link

how i call _getData() again when return in snapshot.hasError ? thank you

@hyochan
Copy link

hyochan commented Jul 16, 2018

@JosephDunivan
Copy link

What if I want to add this into a column with a row that has some buttons in it? I keep getting render errors that no size is set and all kinds of other garbage print outs from my debug console.

@japegamx
Copy link

Hi there,

I tried this code and looks fine, but I did not get the data at the first load. I mean, when I call the widget with this code, it does nothing. But if I click on the hot reload button on Android Studio it loads the data. So, I think that there is something missing for me, because I got the impression that the futureBuilder does not triggers the first time it loads the first State of the StatefulWidget. I'm loading data from a WebService with http library. This load is working fine. Also, the text of "loading..." does not appears, only when I click on hot reload button...

@Mr-Lion
Copy link

Mr-Lion commented Jul 17, 2019

What a shit here. That does not work.
I get "EXCEPTION CAUGHT BY RENDERING LIBRARY" constantly ejected and nothing is displayed.
Why is faulty code being presented on the internet? Idiot. Make it clean first and then show people something!

Copy link

ghost commented Oct 23, 2019

if there is no data
then display error

@hasenbalg
Copy link

thanks, the column helping me out. great example

@hikayat016
Copy link

I had the same problem with sizing to infinite, maybe using an expanded widget on ListView.builder or add shrinkwrap functions in ListView.builder will help you out, or both of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment