我在構建widget時遇到了這個小問題。我想讓它根據firebase的結果顯示圖片,但我無法呼叫小組件imagetniveis (context)。結果是"'imagetniveis'宣告沒有被參考"
是否有其他方法來做我想做的事? 或者我的小組件做錯了什么?
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
標題。Text("Nivel"),
),
body: 容器(
孩子。列(
兒童。[
padding(
padding: const EdgeInsets.only(top: 30.0)。
孩子。SizedBox(
width: 400.0,
高度。70.0。
孩子。FloatingActionButton.extended(
onPressed: () {
printFirebase()。
},
標簽。文本(
'VIZUALIZAR NIVEL DO RIO'。
textScaleFactor: 2,
),
backgroundColor: Color(0xFF17A0A6),
foregroundColor: Colors.white,
),
),
),
**imagemtniveis(context)**
],
),
),
);
}
printFirebase() async {
databaseRef.child('Nivel/').once().then((DataSnapshot snapshot) {
String a = snapshot.value.toString()。
String baixo = '1'/span>;
String medio = '2'。
String alto = '3'。
Widget imagemtniveis(BuildContext context) {
小工具的孩子。
print('${snapshot.value}') 。
if (a == alto) {
child = Padding(
padding: const EdgeInsets.only( top: 10, left: 20)。)
孩子。Image.asset(
'assets/alto.png'。
高度。300。
適合。BoxFit.fill。
),
);
} else if (a == medio) {
child = Padding(
padding: const EdgeInsets.only(top: 10, left: 20)。)
孩子。Image.asset(
'assets/medio.png'。
高度。300。
適合。BoxFit.fill。
),
);
} else {
child = Padding(
padding: const EdgeInsets.only( top: 10, left: 20)。)
孩子。Image.asset(
'assets/baixo.png'。
高度。300。
適合。BoxFit.fill。
),
);
}
return new Container(child: child)。
}
});
}
}
CodePudding
你不能呼叫imagemtniveis,因為它被定義在printFirebase里面。
因此,我已經添加了一個未來,從資料庫中獲取資料并建立你的小部件。
請容忍我,我對實時資料庫并不完美。
@override
build(BuildContext context) {
return Scaffold(
appBar: AppBar(
標題。Text("Nivel"),
),
body: 容器(
孩子。列(
兒童。[
padding(
padding: const EdgeInsets.only(top: 30.0)。
孩子。SizedBox(
width: 400.0,
高度。70.0。
孩子。FloatingActionButton.extended(
onPressed: () {
printFirebase()。
},
標簽。文本(
'VIZUALIZAR NIVEL DO RIO'。
textScaleFactor: 2,
),
backgroundColor: Color(0xFF17A0A6),
foregroundColor: Colors.white,
),
),
),
FutureBuilder<DataSnapshot>(
future: databaseRef.child('Nivel/').once()。
builder: (BuildContext context, DataSnapshot snapshot) {
if (snapshot.hasError) return Message()。
if (snapshot.connectionState == ConnectionState.waiting)
return Loading()。
print('${snapshot.data}') 。
print('${snapshot.data.value}')。
String a = snapshot.data.value.toString()。
String baixo = '1'/span>;
String medio = '2'。
String alto = '3'。
if (a == alto) {
return Container(
孩子。padding(
padding: const EdgeInsets.only(top: 10, left: 20)。)
孩子。Image.asset(
'assets/alto.png'。
高度。300。
適合。BoxFit.fill。
),
),
);
} else if (a == medio) {
return Container(
孩子。padding(
padding: const EdgeInsets.only(top: 10, left: 20)。)
孩子。Image.asset(
'assets/medio.png'。
高度。300。
適合。BoxFit.fill。
),
),
);
} else {
return Container(
孩子。padding(
padding: const EdgeInsets.only(top: 10, left: 20)。)
孩子。Image.asset(
'assets/baixo.png'。
高度。300。
適合。BoxFit.fill。
),
),
);
}
},
),
],
),
),
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/325752.html
標籤:
