I'm having this little problem in my widget build. I'd like it to display the image according to the firebase result, but i can't call the widget imagetniveis (context)
. The result is that "The 'imagetniveis' declaration doesn't is referenced."
Is there another way to do what I'm trying to do? or What am I doing wrong with my widget?
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Nivel"),
),
body: Container(
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 30.0),
child: SizedBox(
width: 400.0,
height: 70.0,
child: FloatingActionButton.extended(
onPressed: () {
printFirebase();
},
label: Text(
'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';
String medio = '2';
String alto = '3';
Widget imagemtniveis(BuildContext context) {
Widget child;
print('${snapshot.value}');
if (a == alto) {
child = Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/alto.png',
height: 300,
fit: BoxFit.fill,
),
);
} else if (a == medio) {
child = Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/medio.png',
height: 300,
fit: BoxFit.fill,
),
);
} else {
child = Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/baixo.png',
height: 300,
fit: BoxFit.fill,
),
);
}
return new Container(child: child);
}
});
}
}
CodePudding user response:
You cannot call imagemtniveis
because it is defined inside printFirebase
.
So rather, I have added a future, to get the data from database and build your widget.
Please bear with me, I am not perfect with realtime database.
@override
build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Nivel"),
),
body: Container(
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 30.0),
child: SizedBox(
width: 400.0,
height: 70.0,
child: FloatingActionButton.extended(
onPressed: () {
printFirebase();
},
label: Text(
'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';
String medio = '2';
String alto = '3';
if (a == alto) {
return Container(
child: Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/alto.png',
height: 300,
fit: BoxFit.fill,
),
),
);
} else if (a == medio) {
return Container(
child: Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/medio.png',
height: 300,
fit: BoxFit.fill,
),
),
);
} else {
return Container(
child: Padding(
padding: const EdgeInsets.only(top: 10, left: 20),
child: Image.asset(
'assets/baixo.png',
height: 300,
fit: BoxFit.fill,
),
),
);
}
},
),
],
),
),
);
}