Im trying to use Firestore on my flutter app but firebase is not running in the main thread
It actually works setting the docs but i get an error showing on each transaction
[ERROR:flutter/shell/common/shell.cc(1055)] The 'plugins.flutter.io/firebase_firestore/transaction/ac27d098-dd91-4050-b7a4-8fe66de5ab48' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel. See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.
in my console and that must be bad!
this is the code
Future<void> initializeFirestore() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}
class User {
String id;
String name;
int age;
User(this.id, this.name, this.age);
Map<String, dynamic> toMap() => {'id': id, 'name': name, 'age': age};
}
Future<void> addUser(User user) async {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
CollectionReference userCollection = firestore.collection('users');
final docRef = FirebaseFirestore.instance.collection('users').doc(user.id);
bool fail = false;
try {
await FirebaseFirestore.instance.runTransaction((transaction) async {
print('transactioning!');
final snapshot = await transaction.get(docRef);
if (snapshot.exists) {
fail = true;
return;
}
print('transactioning okay!');
transaction.set(docRef, user.toMap());
});
if (fail) {
print('transactioning error!');
throw ItemAlreadyExists(user.id);
}
print('User added successfully!');
} on ItemAlreadyExists catch (e) {
print('failed to add ${e.message}');
} catch (e) {
print('Error adding user: $e');
}
}
Future<void> testFirestore() async {
final user1 = User('123', 'Pepe', 20);
final user2 = User('456', 'Rodolfo', 20);
final user3 = User('123', 'Juan', 20);
try {
print('doing 1');
await addUser(user1);
print('done 1, doing 2');
await addUser(user2);
print('done 2, doing 3');
await addUser(user3); // This will throw an ItemAlreadyExists exception
print('done 3');
} catch (e) {
print(e);
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirestore();
runApp(const MyApp());
}
my app has a button
floatingActionButton: FloatingActionButton(
onPressed: () => testFirestore(),
child: const Icon(Icons.add),
),
i cant tell why not even that way it is not executed on main thread, im sus of being a conflictive version firestore but everything is on its latests stable possible version
- using async await: changes nothing
- microtransaction: changes nothing
- post frame listener: changes nothing
- isolate: wont detect the initilized version of firebase and i think i saw in the documentation not to initialize more than one of those
Ema_S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.