Flutter Unhandled Exception: LateInitializationError: Local 'name' has not been initialized - firebase

I have a flutter app which tries to compare two sets of app version numbers, the first version number is stored locally using hive and the second version number is from firestore. I can fetch the data from firestore but I cannot get to compare both since it takes a while to fetch data from firestore.
This is the code to fetch data from firestore
late final Box detailsBox;
#override
void initState() {
super.initState();
detailsBox = Hive.box('appDetails');
updateApplication();
}
CollectionReference groceries =
FirebaseFirestore.instance.collection('updates');
late String? name;
late String? version;
late String? downloadUrl;
getData() {
groceries.orderBy('name').snapshots().listen((gets) {
try {
for (var gettt in gets.docs) {
name = gettt['name'] ?? 'null';
version = gettt['version'] ?? 'null';
downloadUrl = gettt['download url'] ?? 'null';
debugPrint('name: $name');
debugPrint('version: $version');
debugPrint('downloadUrl: $downloadUrl');
_addInfo(name!, version!, downloadUrl!); }
} catch (e) {
print(e);
}
});
}
This is the code to compare the version numbers
int getExtendedVersionNumber(String version) {
List versionCells = version.split('.');
if (kDebugMode) {
print(versionCells);
}
versionCells = versionCells.map((i) => int.parse(i)).toList();
return versionCells[0] * 10000 + versionCells[1] * 100 + versionCells[2];
}
Future compareData() async {
await getData();
String localName = detailsBox.get('name');
String localVersion = detailsBox.get('version');
String downloadLink = detailsBox.get('downloadLink');
debugPrint(
'Info retrieved from detailsBox below:\n $localName\n ($localVersion) \n $downloadLink');
debugPrint(
'Info retrieved from firebase below:\n $name\n ($version) \n $downloadUrl');
int version1Number = getExtendedVersionNumber(localVersion); // return 102003
int version2Number = getExtendedVersionNumber(version!); // return 102003
if (kDebugMode) {
print(version1Number == version2Number);
print(version1Number > version2Number);
print(version1Number < version2Number);
}
if (version2Number > version1Number) {
debugPrint('true');
debugPrint(downloadUrl);
}
}
When it gets to this point debugPrint( 'Info retrieved from firebase below:\n $name\n ($version) \n $downloadUrl'); I get the late initialization error [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: LateInitializationError: Field 'name' has not been initialized.
How can I modify the code such that when it runs I can account for the time it takes to get data then finally compare the versions

Just change
late String? name;
to
String? name;

Related

How to sequentially call the Getx controllers

My Task is :
I have an list of orders in firebase in users collection ,
I want to get information of those orders which is in orders collection,
And moment a list of order is updated in the firebase users collection.
The orders list should be automatically updated to get the latest information from the orders collection.
But for that i have to wait until list of orders is fetched from the users collection and only then i can query from orders collection about those orders..
I am stuck here,
And i want to actually understand Getx's bindStream , ever(), and observable variables,and Obx() is used in widget , But what if it is normal variable and i want to listen to it's changes ,how to do that, because Obx() can be only used while you use Widget
So far my code:
controllers.dart
UtilityController utilityController = UtilityController.instance;
CartController cartController = CartController.instance;
OrderController orderController = OrderController.instance;
UserModel.dart
class UserModel {
String? uid;
String? email;
String? name;
bool? isAdmin;
String? password;
List<CartItemModel>? cart;
String? token;
List<String>? orders;
UserModel({this.uid, this.email, this.name, this.isAdmin, this.password, this.cart, this.token, this.orders});
UserModel.fromSnapshot(DocumentSnapshot snapshot) {
uid = snapshot.id;
name = snapshot['name'];
token = snapshot['token'];
cart = _convertCartItems(snapshot['cart'] ?? []);
orders = new List<String>.from(snapshot['orders']);
}
List<CartItemModel> _convertCartItems(List cartFomDb) {
List<CartItemModel> _result = [];
if (cartFomDb.length > 0) {
cartFomDb.forEach((element) {
_result.add(CartItemModel.fromMap(element));
});
}
return _result;
}
}
UtilityController.dart
class UtilityController extends GetxController {
static UtilityController instance = Get.find();
Rx<UserModel> userModel = UserModel().obs;
#override
void onReady() {
super.onReady();
getUserType();
userModel.bindStream(listenToUser());
}
Stream<UserModel> listenToUser() {
return FirebaseFirestore.instance
.collection("users")
.doc(FirebaseAuth.instance.currentUser?.uid)
.snapshots()
.map((snapshot) => UserModel.fromSnapshot(snapshot));
}
OrderController.dart
class OrderController extends GetxController {
static OrderController instance = Get.find();
RxList<OrderModel> orders = RxList<OrderModel>([]);
#override
void onReady() {
super.onReady();
orders.bindStream(getAllOrders());
ever(utilityController.userModel, function); --> I am using this , but i know this is not the correct way
}
function(UserModel userModel) {
getAllOrders();
}
Stream<List<OrderModel>> getAllOrders() {
return FirebaseFirestore.instance
.collection("orders")
.where(FieldPath.documentId, whereIn: utilityController.userModel.value.orders)
.snapshots()
.map((query) => query.docs.map((item) => OrderModel.fromMap(item.data(), item.id)).toList());
}
}
The utilityController.userModel.value.orders is null !!! it's not yet loaded, so all the orders are fetched :(
And even if the orders are changed... But new orders are not fetched from the orders collection
How to get over this?
Consider using like this.
class OrderController extends GetxController {
static OrderController instance = Get.find();
final isLoading = true.obs;
final isDone = false.obs;
var orders = OrderModel([]).obs;
#override
void onInit() async {
await listenForOrders();
super.onInit();
}
Future listenForOrders() async {
isLoading.value = true;
isDone.value = false;
Stream<QuerySnapshot> _userOrders;
_userOrders = getAllOrders();
_userOrders.listen((QuerySnapshot query) {
if (query.docs.isNotEmpty) {
query.docs.forEach((element) {
orders.addIf(!orders.contains(element), OrderModel.fromDocumentSnapshot(element));
});
} else {
isDone.value = true;
}
isLoading.value = false;
});
}
Stream<QuerySnapshot> getAllOrders() {
return FirebaseFirestore.instance
.collection("orders")
.where(FieldPath.documentId, whereIn: utilityController.userModel.value.orders)
.snapshots();
}
}
The best approach is to use the worker functions provided by getx controller like:
ever - is called every time the Rx variable emits a new value.
everAll - Much like ever , but it takes a List of Rx values Called every time its variable is changed. That's it.
once - is called only the first time the variable has been changed.

"native "AssertionError_throwNew" " error with Firebase Firestore

I have this function that checks if a document exists or not, if exists returns true otherwise false :
Future<bool> checkMissingId(String id) async {
String str = id.toLowerCase();
String letter = str[0];
String path = letter + "/" + str;
try {
final snapShot =
await FirebaseFirestore.instance.collection(path).doc(str).get();
if (snapShot == null || !snapShot.exists) {
return true;
} else
return false;
} catch (e) {
print(e);
return false;
}
}
But when I call it from here (after save and validate form ) :
Future<void> _submit() async {
//Create Artist and send it to the database
if (_validateAndSaveForm()) {
await checkMissingId(userNameF);
}
}
All freezes and it opens a new file called "errors_patch.dart" with this exception :
static _doThrowNew(int assertionStart, int assertionEnd, Object? message)
native "AssertionError_throwNew";
I think the problem is that checkMissingId is of type Future and maybe I'm not handling futures in the right way...But the error highlights also await FirebaseFirestore.instance.collection(path).doc(str).get(); so I don't know exactly how isolate the problem.
This is the stack :
assert(isValidCollectionPath(collectionPath),
await FirebaseFirestore.instance.collection(path).doc(str).get();
with a red quotes :
_AssertionError ('package:cloud_firestore/src/firestore.dart': Failed assertion: line 74 pos 12: 'isValidCollectionPath(collectionPath)': a collection path must point to a valid collection.)
await checkMissingId(userNameF);
Maybe it depends by wrong document path from Firestore?
I think you are passing the wrong collection name in this line:
final snapShot = await FirebaseFirestore.instance.collection(path).doc(str).get();
because, your path variable is this:
String path = letter + "/" + str; // I guess here is something wrong
Your collection name would be a fixed string as shown below in this screenshot (comments, posts, users etc):

Flutter: SQLite database in external storage. database is null

I am writing an app to store some events and dates at which events happened (For history students as part of a project). I wish to create an SQLite database, stored in some preferred location in my device. Later I want this database to merge with main database using computer. Here is my database helper class;
class DatabaseHelper {
static final _dbName = 'mainDatabase.db';
static final _dbVersion = 1;
static final _mainTable = 'mainTable';
static final _storyTable = 'storyTable';
static final _topicTable = 'topicTable';
static final columnId = '_id';
static final title = 'title';
static final description = 'description';
static final parentId = 'topic_id';
static final priority = 'priority';
static final iconId = 'iconId';
static final futureImage = 'image';
static final year = 'year';
static final month = 'month';
static final day = 'day';
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await _checkPermission();
return _database;
}
var status;
_checkPermission() async {
this.status = await Permission.storage.status;
debugPrint(' storage permission status : ${this.status}');
if (await Permission.storage.request().isGranted) {
_initiateDatabase();
} else if (await Permission.storage.request().isUndetermined) {
debugPrint('Undetermined permission');
} else if (await Permission.storage.request().isDenied) {
debugPrint('Permission denied');
_checkPermission();
} else if (await Permission.storage.request().isPermanentlyDenied) {
debugPrint(' it has been permenantly denied');
}
}
_initiateDatabase() async {
debugPrint(' database initialized');
Directory directory = await getExternalStorageDirectory();
String path = join(directory.path, _dbName);
debugPrint('Path for database: ${path}');
return await openDatabase(path, version: _dbVersion, onCreate: _onCreate);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $_mainTable (
$columnId INTEGER NOT NULL,
$year INTEGER,
$month INTEGER,
$day INTEGER,
$title TEXT NOT NULL,
$description TEXT,
$priority INTEGER,
$parentId INTEGER,
PRIMARY KEY($columnId AUTOINCREMENT)
);
'''); /
Future<int> insertEvent(Map<String, dynamic> row) async {
Database db = await instance.database;
debugPrint(' event : ${row}');
assert(db != null); //Issue!!!!
return await db.insert(_mainTable, row);
}
}
My code may seem messy, because I am not from this background. I apologize for that.
When I tried to add an event I get this error;
I/flutter ( 8090): event id is null
I/flutter ( 8090): storage permission status : PermissionStatus.granted
I/flutter ( 8090): database initialized
I/flutter ( 8090): event : {year: null, month: null, day: null, title: adsfasdf, description: null, priority: 0, topic_id: null}
E/flutter ( 8090): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:thisDay/config/databaseHelper.dart': Failed assertion: line 189 pos 12: 'db != null': is not true.
/////////////// some code
E/flutter ( 8090):
I/flutter ( 8090): Path for database: /storage/emulated/0/Android/data/com.example.thisDay/files/mainDatabase.db
What should I do? I am stuck at this point. I have my db file at /storage/emulated/0/Android/data/com.example.thisDay/files/mainDatabase.db . But can't add anything in it.
Any help would be greately appreciated.
Nb:- At first I used getApplicationDirectory instead of External directory. There were no issues. I have to switch because of portability issues
Finally, I fixed my issue by myself. _initializeDatabase() has a return type Future<Database>. So making respective changes on code fixed my issue.
Future<Database> _checkPermission() async {
this.status = await Permission.storage.status;
debugPrint(' storage permission status : ${this.status}');
if (await Permission.storage.request().isGranted) {
return await _initiateDatabase();
} else if (await Permission.storage.request().isUndetermined) {
debugPrint('Undetermined permission');
} else if (await Permission.storage.request().isDenied) {
debugPrint('Permission denied');
_checkPermission();
} else if (await Permission.storage.request().isPermanentlyDenied) {
debugPrint(' it has been permenantly denied');
}
}
Future<Database> _initiateDatabase() async {
debugPrint(' database initialized');
Directory directory = await getExternalStorageDirectory();
String path = join(directory.path, _dbName);
debugPrint('Path for database: ${path}');
return await openDatabase(path, version: _dbVersion, onCreate: _onCreate);
}
for creating a database and table you can refer this code
Database database;
opendatabase() async {
// Delete the database
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, "sample.db");
// await deleteDatabase(path);
database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await db.execute(
'CREATE TABLE tbl_test (id INTEGER PRIMARY KEY, sample TEXT, code TEXT, format TEXT)',
);
});
}

sqflite broke on Android after adding a new table Flutter

I just added a new "Translations" table to the database init and wrote all the crud methods. Commands db.query and db.insert work just fine, but as tried inside updateVersion() either db.delete or db.update throw the missing plugin error :
ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method update on channel com.tekartik.sqflite).
After a while I realised that also older two tables are now throwing the same error on delete and update command. Looks like adding a new table broke it all.. I tried flutter clean but nothing changed, so I uninstalled and reinstalled the app, but I still get same errors. I then commented out new table and reinstalled the app but still get the errors..I also tried invalidating the cache a restart but still the same..
On iPhone dough it does not throw any error..
In iOS CoreData kinda behaves the same when you change something.. but with a fresh app install it all resets.. here it seems that the db is still written on disk..
How do I make sure I erased it? I tried await deleteDatabase(path);.
Any Idea of what's going on?
As always thank you very much for your time and help.
This is the db:
class DBProvider {
//1.Create a private constructor that can be used only inside the class :
DBProvider._();
static final DBProvider db = DBProvider._();
//2.Setup the database
//Next we will create the database object and provide it with a getter
//where we will instantiate the database if it’s not (lazy initialization).
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// await Sqflite.devSetDebugModeOn(true);
// if _database is null we instantiate it
_database = await initDB();
return _database;
}
// If there is no object assigned to the database,
// we use the initDB function to create the database.
// In this function, we will get the path for storing the database
// and create the desired tables:
initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "Fixit.db");
//TODO: tried deleting
// await deleteDatabase(path); // still errors
return await openDatabase(path, version: 1, onOpen: (db) {},
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE Route("
"routeId TEXT,"
"routeName TEXT,"
"routeDistance TEXT,"
"routeDuration TEXT,"
"coordinates TEXT"
")");
await db.execute("CREATE TABLE Alarm("
"alarmId TEXT,"
"alarmName TEXT,"
"enabled BOOL,"
"repeatWeekdays TEXT,"
"time TEXT,"
"sound TEXT,"
"routeName TEXT"
")");
// await db.execute("CREATE TABLE Translations("
// "version TEXT"
// ")");
});
}
}
and these are the CRUD methods:
class DefaultsDbRepository {
var database = DBProvider.db.database;
Future<int> checkVersion() async {
final db = await database;
try {
var res = await db.query('Translations');
assert(res != null);
int version = res != null ? int.parse(res.first['version']) : 0;
print('checkVersion() db version is: $version');
return version;
} catch (err) {
print(err);
}
}
saveVersion({int version}) async {
print('saveVersion() version to save is : $version');
assert(version != null);
final db = await database;
try {
Map<String, dynamic> map = {'version': version};
db.insert("Translations", map);
} catch (e) {
print(e);
}
}
updateVersion({int newVersion, int oldVersion}) async {
print('updateVersion() version to save is : $newVersion');
assert(newVersion != null);
final db = await database;
db.delete("Translations");
try {
// Map<String, dynamic> map = {'version': newVersion};
//
// db.insert("Translations", map);
// db.update("Translations", map); // [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method update on channel com.tekartik.sqflite)
// db.update("Translations", map,
// where: "version = ?", whereArgs: [oldVersion]);
} catch (e) {
print(e);
}
}
}
Finally made it work again. After quite a few, uninstall, flutter clean and reinstalls I saw the problem. I was using ' instead of " in my db methods.
So I changed'ed from:
saveVersion({int version}) async {
print('saveVersion() version to save is : $version');
assert(version != null);
final db = await database;
try {
Map<String, dynamic> map = {'version': version};
db.insert("Translations", map);
} catch (e) {
print(e);
}
}
to:
saveVersion({int version}) async {
print('saveVersion() version to save is : $version');
assert(version != null);
final db = await database;
try {
Map<String, dynamic> map = {"version": version};
db.insert("Translations", map);
} catch (e) {
print(e);
}
}
and all commands are working as expected.
Hope this will be of help to others.
Cheers.

Flutter Getting a specific Firestore document data and store it into a String variable

I am trying to get data from my Firestore and store the data into a String variable named time. Then put the variable into _event.
However, when I run the code, I got null, and I don't know why.
StateFulWidget...
String event;
Map<DateTime, List> _events;
#override
void initState() {
super.initState();
Firestore.instance.collection('events').document('2019-07-
30').get().then((
DocumentSnapshot ds) {
event = ds['time'];
});
_events = {
DateTime.parse("2019-08-01"): [event]
};
}
This is my Firestore document
I am new to Flutter and I have struggled for this question for a long time. Hope someone can help me
Thank you so much
UPDATE
I change my code.
However, I am still getting errors: The method [](Intance of 'DateTime') was called on null.
String event;
Map<DateTime, List> _events;
Future<String> getData()async{
final ds = await
Firestore.instance.collection('events').document('2019-07-30').get();
return ds['time'];
}
#override
void initState() {
super.initState();
getData().then((s) {
event = s;
//when I print(s), it showed the data correctly
});
_events = {
DateTime.parse("2019-08-01"): [event]
};
Can someone shows me where I got wrong?
Thank you!
You are not waiting for the result, ie _events = .. will run before .then() clause and that is why event variable is still null;
.get() is a Future so you need to await the result.
final ds = await Firestore.instance.collection('events').document('2019-07-
30').get();
event = ds['time'];
_events = {
DateTime.parse("2019-08-01"): [event]
};
This works for me :
Firestore().instance.collection('Collection Name')
.document('Document Name').get().then((data) async {
var dataReceive = data['property Name'];
}

Resources