Мне нужна помощь в том, как получить конкретные данные из "FirebaseFirestore" и присвоить их переменной. Затем я напишу команду if и else и т. д. Я искал решения, но у меня ничего не получилось. У меня есть 5 виджетов «Карточка», все они одинаковы, за исключением их изображения и текста, здесь мы собираемся использовать нашу переменную. Кстати, данные будут поступать из поля («имя_коллекции/имя_документа/поле№_1»). Вот виджет «Карта». пример кода:
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/brick-pile.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
Мне нужно использовать переменную в дочернем виджете "Текст", где указано "14". и вот полный код страницы:
Widget build(BuildContext context) {
height = MediaQuery.of(context).size.height;
width = MediaQuery.of(context).size.width;
//Resource variables
return Scaffold(
body: Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
height: height * .5,
decoration: BoxDecoration(
color: PlayerColorWGrey,
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(50),
),
),
),
Container(
decoration: BoxDecoration(
color: PlayerColorWGrey,
),
child: Container(
height: height * .5,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(50),
)),
),
),
],
),
Column(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(vertical: 50.0, horizontal: 2.5),
height: height * .2,
width: width,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/stone-pile.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/brick-pile.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/sheep.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/wheat.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
Card(
color: Colors.black,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
elevation: 7.5,
margin:
EdgeInsets.only(left: 1, right: 1, top: 8, bottom: 8),
child: InkWell(
borderRadius: BorderRadius.circular(20),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SvgPicture.asset(
"assets/icons/wood-pile.svg",
height: 75,
width: 50,
semanticsLabel: "Resource Card",
),
Text("14",
style: new TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w700,
fontFamily: "Cairo",
color: Colors.white)),
],
),
),
),
),
заранее спасибо
Редактировать 1: я создаю функцию и вот код:
getData() {
FirebaseFirestore.instance
.collection("catanR")
.doc('Player One White')
.get()
.then((value) {
setState(() {
wheat = value.data()!['wheat'];
stone = value.data()!['stone'];
brick = value.data()!['brick'];
wood = value.data()!['wood'];
sheep = value.data()!['sheep'];
});
});
}
Я думаю, что это работает, но я не мог попробовать, потому что, как я сказал в начале, я буду использовать эти переменные в виджете Text. Поэтому я должен сначала вызвать функцию, а затем запустить функцию, но все, что мне нужно, это то, что эти переменные должны быть видны постоянно.
Изменить 2: структура базы данных для @Technical World:
Посмотрите в этих похожих темах. Решение в приведенных ниже темах включает async, await, а затем ключевые слова, которые могут помочь. [1]. … [4] coderhelper.com/questions/62823096/…