how do I make a method inside Future class? I wanted to call the methods in other dart files, but because I made the functions as Future, it does not get called by the instance name.
This is the example code that I want to call in another dart file.
Future tokenDb() async{
final database = openDatabase(
join(await getDatabasesPath(), 'token_list.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE tokens (token INTEGER PRIMARY KEY)",
);
},
version: 1,
);
Future<void> insertToken(Token token) async {
final Database db = await database;
await db.insert(
'tokens',
token.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
}
and I need it here:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
MaterialApp(home: MyApp()),
);
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late FirebaseMessaging messaging;
String tokenValue = "";
@override
void initState() {
messaging = FirebaseMessaging.instance;
messaging.getToken().then((value) {
tokenValue = value!;
Clipboard.setData(ClipboardData(text: tokenValue));
print(tokenValue);
var user1 = Token(token: tokenValue);
print("user1 token : " + tokenValue);
**var db = tokenDb();
db.insertToken(user1);
tokenDb();**
// Maybe I need to call the function here?
});
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: AuthTypeSelector(),
),
);
}
}
I don't have a lot of knowledge of Flutter... Thanks for your help!! I really appreciate it. :)