为什么每次我访问站点时都要执行从服务器检索数据的功能?
会有人知道如何通过仅第一次下载信息然后仅在发生更改时才重新加载信息来改变这种情况吗?
我使用此主题来编写我的功能:https://github.com/flutter/flutter/issues/11426#issuecomment-414047398
感谢您的帮助:)
我的代码:
class _MyFollowsState extends State<MyFollows> {
UserApiService userApiService;
SharedPreferences sharedPreferences;
Future _future;
@override
void initState(){
super.initState();
userApiService = UserApiService();
_future = getUserData();
}
getUserData() async{
sharedPreferences = await SharedPreferences.getInstance();
int id = sharedPreferences.getInt('id');
return userApiService.getUser(id);
}
@override
Widget build(BuildContext context){
return FutureBuilder(
future: _future,
builder: (context, snapshot){
if(!snapshot.hasData){
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: CircularProgressIndicator(),
),
);
}
User user = snapshot.data;
return Scaffold(
backgroundColor: Colors.white,
body: Container(
child: ListView.builder(
itemCount: user.myFollows.length,
itemBuilder: (context, i){
return GestureDetector(
onTap: () {
//
},
child: Card(
margin: EdgeInsets.only(top: 15, right: 10, left: 10),
child: ListTile(
title: Text(user.myFollows[i].firstName + ' ' + user.myFollows[i].lastName),
subtitle: Text(user.myFollows[i].accountName),
leading: CircleAvatar(
radius: 25,
backgroundImage: NetworkImage('https://upload.wikimedia.org/wikipedia/commons/8/8d/George_Clooney_2016.jpg'),
),
trailing: Icon(Icons.more_vert),
),
)
);
}
)
),
);
},
);
}