我有以下代码,这些代码是我从API接收的带有帖子的列表,当我打开应用程序时,如果我再次保存该代码,则列表会正确显示,但每次重新启动应用程序时,列表都是空的, 有任何想法吗? 我是新手,所以如果我有些愚蠢,请原谅我
我正在使用VS,并且通过VS调试器运行该应用程序,我也没有收到任何错误,也许列表不可用?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter1/screen/login.dart';
import 'package:flutter1/network_utils/api.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:developer' as developer;
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home>{
//final List<String> people = ['george','evripides','vicky'];
// var posts;
List<dynamic> posts = [];
String name;
@override
void initState(){
_loadUserData();
_loadPosts();
super.initState();
}
_loadUserData() async{
SharedPreferences localStorage = await SharedPreferences.getInstance();
var user = jsonDecode(localStorage.getString('user'));
//developer.log(user);
if(user != null) {
setState(() {
name = user;
});
}
}
Future _loadPosts() async{
await Network().getDataPosts('post/getallposts?page=1').then((res) {
developer.log("home"+ res.body);
var posts1 = jsonDecode(res.body);
print(posts1);
posts = posts1['data']['data'];
print(posts[1]['title']);
print(posts.length);
return posts.toList();
},
onError: (error) {
print(error);
});
//var res = await Network().getDataPosts('post/getallposts?page=1');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test App'),
backgroundColor: Colors.teal,
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Hi, $name',
style: TextStyle(
fontWeight: FontWeight.bold
),
),
new Expanded(
child: posts.isEmpty ? Center(child: Text('Empty')) : ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
var item = posts[index]['title'];
var item2 = posts[index]['comments'];
var item3 = posts[index]['country'];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 10.0,
child: ListTile(
title: Text('$item ${item3 == 'Cyprus' ? true : item3}'),
subtitle: Text(item2),
trailing: GestureDetector(
child: Icon(
Icons.dehaze,
color: Colors.black,
),
onTap: () {
setState(() {
posts.remove(item);
});
}),
),
),
);
}),
),
Center(
child: RaisedButton(
elevation: 10,
onPressed: (){
logout();
},
color: Colors.teal,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
child: Text('Logout'),
),
),
],
),
),
);
}
void logout() async{
// var res = await Network().getData('/logout');
// var body = json.decode(res.body);
//if(body['success']){
SharedPreferences localStorage = await SharedPreferences.getInstance();
localStorage.remove('user');
localStorage.remove('token');
Navigator.push(
context,
MaterialPageRoute(builder: (context)=>Login()));
}
}
答案 0 :(得分:1)
由于从服务器获取数据是异步任务,因此需要花费一些时间才能完成,因此,当您获取数据时,必须调用setState而不是调用setstate,因此您将面临此行为。
setState((){
posts = posts1['data']['data'];
});