I am working on network requests within flutter using the flutter http package.
So far I have a welcome screen and a network helper.
The welcome screen has a scaffold with a list view and a bottom bar.
The list view will show text and an image per every list item.
The data for these items comes from an HTTP request.
That request is done in my network helper class.
In the welcome screen I have a function that I call from initState() that function will set a local variable to the json returned from the network helper but once I try to access the data from the variable it is null. However if I instead just print the data then the data is there.
My problem is I have accessed the data but cant think of the way I can pass that data into the list view.
Here is what my network helper class code looks like
import 'package:http/http.dart' as http;
import 'dart:convert';
class NetworkHelper{
//Sets up search filter variables
int year;
String make;
String model;
String condition;
String combustible;
String style;
String color;
double minPrice;
double maxPrice;
Future fetchAlbum() async {
http.Response response = await http.get('https://jsonplaceholder.typicode.com/todos');
if (response.statusCode == 200) {
String data = response.body;
return jsonDecode(data);
} else {
throw Exception('Failed to load album');
}
}
}
And here is what my welcome screen looks like
import 'package:flutter/material.dart';
import 'package:components/appBarTitle.dart';
import 'package:components/bottomBarComponent.dart';
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:constants.dart';
import 'package:helpers/network_helper.dart';
import 'dart:convert';
class WelcomeScreen extends StatefulWidget {
static String id = '/welcome_screen';
@override
_WelcomeScreenState createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
int _index = 0;
NetworkHelper http = NetworkHelper();
dynamic data;
Future testMethod () async {
this.data = await http.fetchAlbum();
print(this.data);
}
@override
void initState() {
super.initState();
testMethod();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0.0,
title: AppBarTitle(),
),
body: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(
children: [
ListBody(
children: [
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
),
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
),
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
),
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
),
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
),
Card(
child: Row(
children: [
Expanded(
child: Column(
children: [
Text('2017 Nissan', style: TextStyle(fontWeight: FontWeight.w300,)),
Text('Versa 1.6 SL', style: TextStyle(fontWeight: FontWeight.w100,)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('\$7,998.00', style: TextStyle(fontWeight: FontWeight.w200,)),
),
],
),
),
Expanded(
child: Image.network('https://via.placeholder.com/450/0000FF', scale: 4,),
),
],
),
)
],
),
],
),
),
bottomNavigationBar: ConvexAppBar(
backgroundColor: Color(0xffe74c3c),
items: [
TabItem(icon: Icons.home, title: 'Home'),
TabItem(icon: Icons.map, title: 'Discover'),
TabItem(icon: Icons.search, title: 'Busca'),
TabItem(icon: Icons.person, title: 'Mi Cuenta'),
TabItem(icon: Icons.build, title: 'Settings'),
],
initialActiveIndex: 2,//optional, default as 0
onTap: (int i) => print('click index=$i'),
)
);
}
}