我有一个future,它提供了一个字符串类型的返回leadid。
Future<String> getleader() async {
final DocumentSnapshot data = await Firestore.instance
.collection('groups')
.document(widget.detailDocument.data['groupId']).get();
String leadid = data.data['leader'];
return leadid;
}
我想在这里使用该值。 ListTile( 标题:Text(getleader()), 开头:Text('Leader:'), ),
它说将来的字符串不能分配给参数字符串。
我也尝试添加一个函数来等待结果,如下所示
getdata2() async {
String lead1= await getleader();
但它也显示了错误未来动力不是字符串类型的子类型
这是我要使用未来值的地方
Widget _memebrprofile() {
return FutureBuilder(
future: getleader(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// store the value of the Future in your string variable
storeValue = snapshot.data;
return storeValue;
}
return Scaffold(
drawer: newdrawer(),
appBar: AppBar(
title: Text('User Details'),
),
body: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(),
child: Column(
children: <Widget>[
ListTile(
title: SelectableText(
widget.detailDocument.data["groupId"] ?? '',
),
leading: Text('Group Id :'),
),
ListTile(
title: Text(storeValue),//this is where i want to display the string
leading: Text('Leader :'),
),
Row(
children: <Widget>[
Flexible(
child: RaisedButton(
onPressed: () {
//this is where i want to use it as a string value to check a certain bool. if (storeValue == _uid()) {
Firestore.instance
.collection('users')
.document(widget.detailDocument.documentID)
.updateData({
'groupId': "",
});
Navigator.of(context).pop();
Navigator.pushNamed(context, assignedTask.id);
} else {}
},
child: Text('Remove user'),
),
),
/* Flexible(
child:RaisedButton(
onPressed: () {
},
child: Text('Changerole to user'),
),),
Flexible(
child: RaisedButton(
onPressed: () {
},
child: Text('Changerole to Admin'),
),
),*/
Flexible(
child: RaisedButton(
onPressed: () async {
FirebaseAuth auth = FirebaseAuth.instance;
final FirebaseUser user =
await auth.currentUser();
final userid = user.uid;
if (widget.detailDocument.documentID == userid) {
Navigator.pushNamed(context, MyProfile.id);
} else {}
},
child: Text('Edit Profile'),
),
),
],
),
],
),
),
),
);
});
}
}
答案 0 :(得分:0)
由于未使用FutureBuilder
,因此出现错误。
尝试使用FutureBuilder
。
您可以通过将小部件包装在FutureBuilder
中来解决此问题。
检查以下代码:可以正常工作。
// use a future builder
return FutureBuilder<String>(
// assign a function to it (your getLeader method)
future: getleader(),
builder: (context, snapshot) {
if(snapshot.hasData){
// print your string value
print(snapshot.data);
return new ListTile(
leading: Text('Leader'),
title: Text(snapshot.data),
onTap: () {
}
);
} else {
return Text(snapshot.error.toString());
}
}
);
我希望这会有所帮助。
已更新 根据要求将值(字符串)存储到变量中,请检查以下代码:
// declare your variable
String storeValue;
return FutureBuilder<String>(
// assign a function to it (your getLeader method)
future: getleader(),
builder: (context, snapshot) {
if(snapshot.hasData){
// store the value of the Future in your string variable
storeValue = snapshot.data;
return new ListTile(
leading: Text('Leader'),
title: Text(snapshot.data),
onTap: () {
}
);
} else {
return Text(snapshot.error.toString());
}
}
);
答案 1 :(得分:0)
尝试以下操作:
FutureBuilder(
future: getleader(),
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return ListTile(
contentPadding: EdgeInsets.all(8.0),
title:
Text(snapshot.data),
);
});
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
Future<String> getleader() async {
final DocumentSnapshot data = await Firestore.instance
.collection('groups')
.document(widget.detailDocument.data['groupId']).get();
String leadid = data.data['leader'];
return leadid;
}
出现上述错误的原因是因为getleader()
返回了Future<String>
,而Text
小部件的值是String
类型,因此使用了{{1} },那么您可以获得Future的价值并在FutureBuilder
小部件中使用它。
答案 2 :(得分:0)
您可以在StatefulWidget中创建另一个功能,该功能可以使用setState()
更新Lead1。 String lead1 = "";
getLeadID() {
getLeader().then((val) => setState(() {
lead1 = val;
}));
}
.then(val)
等待getLeader()
完成,然后允许您使用返回的值val
。
编辑:
将ListTile中的文本设置为lead1变量,例如
ListTile( title: Text(lead1), leading: Text('Leader :'), ),
然后像这样在initState()中调用getLeadID()
函数;
class _MyHomePageState extends State<MyHomePage> {
String lead1 = "";
@override
void initState() {
super.initState();
getLeadID();
}
@override
Widget build(BuildContext context) {
//rest of code