我正在使用flutter http包处理flutter中的网络请求。
到目前为止,我有一个欢迎屏幕和一个网络助手。
欢迎屏幕有一个带有列表视图和底部栏的脚手架。
列表视图将显示每个列表项的文本和图像。
这些项目的数据来自 HTTP 请求。
该请求是在我的网络助手类中完成的。
在欢迎屏幕中,我有一个从 initState() 调用的函数,该函数将为从网络帮助程序返回的 json 设置一个局部变量,但是一旦我尝试从该变量访问数据,它就为空。但是,如果我只是打印数据,那么数据就在那里。
我的问题是我已经访问了数据,但想不出如何将数据传递到列表视图中。
这是我的网络助手类代码的样子
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');
}
}
}
这是我的欢迎屏幕的样子
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'),
)
);
}
}