So I am currently writing an app which connects to a sensor module through Bluetooth and receives data in the form of a string. The data will contain a UID which will be compared to the key of children in the Firebase Realtime Database. In the case that there is no such UID, we need to create a new child for this UID and for that purpose we need the email of the user. My idea is that in case there is no such child in Firebase, the flutter app will open a dialog box where there will be a text box to enter the email string which will then be recorded into Firebase. The code for the firebase function is as follows and is located on a seperate dart class file other than main.dart.
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'data_processing.dart';
// ALL FUNCTIONS WILL RUN IN ONE LOOP WITHOUT NEEDING FEEDBACK FROM MAIN
//******************************************************************************
class Firebase {
DatabaseReference ref = FirebaseDatabase.instance.ref();
// INITIAL FUNCTION TO SEARCH FOR UID
//****************************************************************************
Future<void> searchByUID(List<int> intList) async {
late String myUID;
if (intList.isNotEmpty) {
myUID = intList.first.toString();
print('UID: $myUID');
} else {
print('The list is empty.');
}
final snapshot = await ref.child('S_ID/$myUID').get();
if (snapshot.exists) {
// UID EXISTS
//************************************************************************
} else {
// NO UID EXISTS
//************************************************************************
String lastUpdatedDate = DataProcessing.getCurrentDate();
int lastUpdatedHour = DataProcessing.getCurrentHour();
String volumeLeft = "350.00";
late String email;
DatabaseReference userRef = FirebaseDatabase.instance.ref("S_ID/$myUID");
await userRef.set({
"Email Address": email,
"Last Updated Date": lastUpdatedDate,
"Last Updated Time": lastUpdatedHour,
"Volume Left": volumeLeft
});
}
}
}
I am yet to write this function for the dialogBox to appear and hence why the email is not filled in yet.I am currently lost on how to implement this. Would I require a Stateless Widget in main.dart to retrieve the data or am I able to carry out the functions in this seperate dart file?
Any help regarding how to start writing this function would be great help.
First things first.
You want to write a flutter app that opens a dialog box when your firebase class doesn't find UID of the object you're looking for. It's ok, but the class that you wrote could be good for backend, but for now you have not a single view. , so you cannot show anything (not a dialog, not a warning). What you should do is writing a class that works as view, and while building it call methods from your firebase class and build widgets depending on what your firebase class responds.
For example:
It's just an incomplete example, but i left you quite all the widget you will need to build your View to what you want. There a LOT here to explain, but i suggest you to learn by yourself watching tutorials, looking at flutter page (where there are tutorial with codes) and trying to build a simple view for yourself first. What you want to do it's not so complicated, but you want to have in your mind what are you doing.