我是 flutter 的新手,我正在尝试在未来构建器中的列表视图构建器中调用 api 数据。但是我在快照中得到了空数据。
我的代码如下:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
String token;
SharedPreferences sharedPreferences;
class ContinueW extends StatefulWidget {
@override
_ContinueWState createState() => _ContinueWState();
}
class _ContinueWState extends State<ContinueW> {
Future getTokenValue() async {
SharedPreferences espf = await SharedPreferences.getInstance();
token = espf.getString('tokenvalue');
return token;
}
Future createUser() async {
String url ='http://3.137.182.252:8000/continue-watching';
print('token');
Map<String, String> headers = {
'token': token,
};
await http.get(url, headers: headers).then((response){
print('Entered');
//print(response.body);
var jre= jsonDecode(response.body);
return jre;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: getTokenValue(),
builder: (BuildContext context,snapshot){
if(snapshot.hasData){
return cbuild();
} else{
return CircularProgressIndicator();
}
},
),
),
);
}
Widget cbuild(){
return new FutureBuilder(
future: createUser(),
builder: (BuildContext context,snapshot) {
if(snapshot.hasData) {
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
backgroundImage: snapshot.data['thumburl'] != null
? NetworkImage(snapshot.data['thumburl'])
: Container(),),
title: Text(snapshot.data[index]['name']),
subtitle: Text(snapshot.data[index]['description']),
);
},
);
}
else{
return CircularProgressIndicator();
}
}
);
}
}
答案 0 :(得分:0)
ListTile(
leading: CircleAvatar(
backgroundImage: snapshot.data[index]['thumburl'] != null
? NetworkImage(snapshot.data[index]['thumburl'])
: Container(),),
title: Text(snapshot.data[index]['name']),
subtitle: Text(snapshot.data[index]['description']),
);
根据您提供的详细信息。你可能会用 ['thumburl'] 丢失 [index],
如果还是不行,那么你需要提供JSON格式的数据