【发布时间】:2021-09-03 03:55:07
【问题描述】:
我想从我创建的 api 中读取列表,但每次我试图从 api 中获取我的列表但我不断收到此错误,我不知道为什么?这个年龄好像有错误,但我不知道错误在哪里,如果有人能帮助我,我将不胜感激。
我想从我创建的 api 中读取列表,但每次我试图从 api 中获取我的列表但我不断收到此错误,我不知道为什么?这个年龄好像有错误,但我不知道错误在哪里,如果有人能帮助我,我将不胜感激。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ApiService api = ApiService();
late List<Track> trackList;
@override
Widget build(BuildContext context) {
if(trackList == null) {
trackList = <Track>[];
}
return Scaffold(
backgroundColor: Color(0xff121219),
appBar: AppBar(
backgroundColor: Color(0xff121219),
actions: [
IconButton(
icon: const Icon(Icons.music_note),
tooltip: 'Increase volume by 10',
onPressed: () {
// Navigator.push(context,
// MaterialPageRoute(builder: (context) {
// return TrackList( track: [],);
// }));
},
),
],
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Image(image: AssetImage('assets/logo.png'), fit: BoxFit.cover, width: 170,),
],
),
),
body: Center(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: <Widget>[
// Container(
// margin: EdgeInsets.only(bottom: 40),
// child: Image(image: AssetImage('assets/vinyl.png'), fit: BoxFit.cover, width: 170,)),
// SizedBox(height: 20),
// Text(
// 'Please tab on the "+" to add tracks',
// style:TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize:14 ),
// ),
// ],
// )
child: trackList.length > 0 ? Container(
child: new Center(
child: new FutureBuilder(
future: loadList(),
builder: (context, snapshot) {
return trackList.length > 0? new TracksList(tracks: trackList):
new Center(child:
new Text('No data found, tap plus button to add!', style: Theme.of(context).textTheme.title));
},
)
),
) : Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 40),
child: Image(image: AssetImage('assets/vinyl.png'), fit: BoxFit.cover, width: 170,)),
SizedBox(height: 20),
Text(
'Please tab on the "+" to add tracks',
style:TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize:14 ),
),
],
)
),
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xffDE1534),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return AddDataWidget();
}));
},
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Future loadList() async {
Future<List<Track>> futureCases = api.getTracks();
futureCases.then((trackList) {
setState(() {
this.trackList = trackList;
});
});
return await futureCases;
}
}
【问题讨论】:
标签: flutter