0
I am making the call in my database of the boarding number, I need to put this number inside a button, but my variable "boarding" appears as undefined inside the Elevatedbutton
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'Embarque.dart';
import 'Settings.dart';
import 'database/sqlite/connection.dart';
class menu extends StatefulWidget {
const menu({Key? key}) : super(key: key);
@override
_menuState createState() => _menuState();
}
String num_embarque = "";
void main() async {
Database db = await Connection.get;
String url = 'SELECT nr_embarque FROM embarque';
List nr_embarque = await db.rawQuery(url);
num_embarque = nr_embarque.first["nr_embarque"];
}
class _menuState extends State<menu> {
@override
Widget build(BuildContext context) {
new Container();
return Scaffold(
appBar: AppBar(
brightness: Brightness.dark,
automaticallyImplyLeading: false,
title: Text("Embarques"),
backgroundColor: Color(0xffb0000CD),
),
body: Container(
padding: EdgeInsets.all(32),
decoration: BoxDecoration(color: Colors.grey[400]),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 1000, height: 50),
child: ElevatedButton(
child: Text(
"Embarque: $num_embarque",
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
style: ElevatedButton.styleFrom(
primary: Color(0xffbDCDCDC),
onPrimary: Colors.black,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Embarque(),
),
);
},
),
),
SizedBox(
height: 32,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Settings(),
),
);
},
child: const Icon(Icons.settings),
backgroundColor: Color(0xffb0000CD),
));
}
}
I changed the way of declaring the variable as you said and change the value of it later, but at the time of displaying is returning null, IE, the change I make occurs only within my async method... I’m doing it this way:
– Igor
edited the question and put the updated code
– Igor
@Igor, the observations I made stick. Because you called the function
main()
? Where are you calling her? If you don’t call her, she will never perform. And if you don’t use thesetState()
, it will never update the screen when finished. Better read the second part of my question, and try that official Google codelab if you want a more practical example. If you have any questions, punctual and objective, can edit here or do another on the site.– Naslausky