问题描述
我正在构建一个按钮列表,使用 Firebase 为每个按钮命名,但我的代码在将“_getbuttonbar”引用到小部件时出现此错误:“无法将参数类型‘Future’分配给参数类型“列表””。小部件也应该是 Future 吗?有人知道缺少什么吗?
class ThemesList extends StatelessWidget {
Future<List<FlatButton>> _getbuttonbar(context) async {
List<FlatButton> _list1 = [];
tot = await callReadTotal(); //Receives length to make the loop.
getListCauses(); //Calls Firebase to use arrCauses = List () for child: Text
for (int i = 1; i <= tot; i++) {
_list1.add(
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),side: BorderSide(color: Color.fromrGBO(150,1,1)),),splashColor: Color.fromrGBO(150,1),onpressed: () => {
setChoice(i),Navigator.push(
context,MaterialPageRoute(builder: (context) => CausesList()),},child: Text(
arrCauses[i],style: TextStyle(
color: Color.fromrGBO(150,fontSize: 20,fontFamily: 'Balsamiq_Sans',);
}
return _list1;
}
@override
Widget build(BuildContext context) {
return Platform.isIOS
? CupertinopageScaffold(child: null)
: Scaffold(
appBar: AppBar(
title: Text(
'ICare',style: TextStyle(
color: Color.fromrGBO(150,fontSize: 40,fontWeight: FontWeight.w500,flexibleSpace: Image(
image: Assetimage('assets/images/solidariedade.png'),color: Color.fromrGBO(255,200,0.45),colorBlendMode: BlendMode.modulate,fit: BoxFit.cover,body: Stack(
children: <Widget>[
Container(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,child: buttonbar(
children: _getbuttonbar(
context),//<-- Here it points out the error.
alignment: MainAxisAlignment.center,],);
}
}
解决方法
您的问题可以通过两种方式解决。
通过使用 StatefulWidget
import 'package:flutter/material.dart';
class ThemesList extends StatefulWidget {
@override
_ThemesListState createState() => _ThemesListState();
}
class _ThemesListState extends State<ThemesList> {
List<FlatButton> _buttonBar = [];
@override
void initState() {
_getButtonBar();
super.initState();
}
_getButtonBar() async {
List<FlatButton> _list1 = [];
tot = await callReadTotal(); //Receives length to make the loop.
getListCauses(); //Calls Firebase to use arrCauses = List () for child: Text
for (int i = 1; i <= tot; i++) {
_list1.add(
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),side: BorderSide(color: Color.fromRGBO(150,1,1)),),splashColor: Color.fromRGBO(150,1),onPressed: () => {
setChoice(i),Navigator.push(
context,MaterialPageRoute(builder: (context) => CausesList()),},child: Text(
arrCauses[i],style: TextStyle(
color: Color.fromRGBO(150,fontSize: 20,fontFamily: 'Balsamiq_Sans',);
}
_buttonBar = _list1;
setState(() {});
}
@override
Widget build(BuildContext context) {
return Platform.isIOS
? CupertinoPageScaffold(child: null)
: Scaffold(
appBar: AppBar(
title: Text(
'ICare',style: TextStyle(
color: Color.fromRGBO(150,fontSize: 40,fontWeight: FontWeight.w500,flexibleSpace: Image(
image: AssetImage('assets/images/solidariedade.png'),color: Color.fromRGBO(255,200,0.45),colorBlendMode: BlendMode.modulate,fit: BoxFit.cover,body: Stack(
children: <Widget>[
Container(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,child: ButtonBar(
children: _buttonBar,alignment: MainAxisAlignment.center,],);
}
}
通过使用 FutureBuilder
import 'package:flutter/material.dart';
class ThemesList extends StatelessWidget {
Future<List<FlatButton>> _getButtonBar(context) async {
List<FlatButton> _list1 = [];
tot = await callReadTotal(); //Receives length to make the loop.
getListCauses(); //Calls Firebase to use arrCauses = List () for child: Text
for (int i = 1; i <= tot; i++) {
_list1.add(
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),);
}
return _list1;
}
@override
Widget build(BuildContext context) {
return Platform.isIOS
? CupertinoPageScaffold(child: null)
: Scaffold(
appBar: AppBar(
title: Text(
'ICare',child: FutureBuilder<List<FlatButton>>(
future: _getButtonBar(context),builder: (context,AsyncSnapshot<List<FlatButton>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
break;
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return ButtonBar(
children: snapshot.data,);
}
}),);
}
}