You can use futurebuilder to call an api. Here I have gave full demonstration how to call api with loader and update view.
dependencies:
flutter:
sdk: flutter
http: "^0.12.0"
after adding dependency import it
import 'package:http/http.dart' as http;
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: ListView(
children:[
updateTopratedMovie(context),
]
),
);
}
Future<dynamic> getTopratedMovie() async {
String url =
'https://api.themoviedb.org/3/movie/top_rated';
http.Response response = await http.get(url);
return json.decode(response.body);
}
Widget updateTopratedMovie(context) {
return FutureBuilder(
future: getTopratedMovie(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
if (snapshot.data != null) {
dynamic content = snapshot.data;
return SizedBox(
height: 500.0,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 0.0),
child: Container(
// elevation: 2.0,
child: ListView.builder(
// scrollDirection: Axis.horizontal,
itemCount: content['results'].length,
itemBuilder: (context, i) =>
Container(
height:100.0,
color:Colors.red
child:Text(i);
),
),
),
);
}
} else {
return Container(
height: 120.0,
width: MediaQuery.of(context).size.width,
child: Center(
child: CircularProgressIndicator(
backgroundColor: Color(0xff00d2ff),
),
),
);
}
},
);
}
}