Home > Mobile >  Results are different when application tested on a physical phone and on simulator
Results are different when application tested on a physical phone and on simulator

Time:11-06

I am trying to solve an issue with my application. When I test it on a virtual device (iPhone), the query is working well and I am getting the document I am supposed to get. When I test the application on my physical phone, the application does not find any record.

I have checked the filters, they are the same. it is exactly the same code. I have never have this situation before. Please, do you have any suggestion?

Future myQuery (  time,  energy,  urgent,  important ) async{
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';
    final currentQuery = FirebaseFirestore.instance.collection(path);

    Query statusQuery = currentQuery.where('status', isEqualTo: 'Inbox');
   // Query contextQuery = statusQuery.where('context', isEqualTo: );
    Query timeQuery = statusQuery.where('time_Needed', isEqualTo: time);
    Query energyQuery = timeQuery.where('energy_Needed', isEqualTo: energy);
    Query urgentQuery = energyQuery.where('urgent', isEqualTo: urgent);
    Query importantQuery = urgentQuery.where('important', isEqualTo: important);

    final snapshot = await importantQuery.get();
    final data = snapshot.docs;

    if(data.isNotEmpty) {
      return snapshot;
    }
  }
 ElevatedButton(child:
                      const Text('FIND'),
                          onPressed: () async {

                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                builder: (context) =>
                                     EngageDisplayTasks(
                                      time: timeSelectedPicker!, energy: energySelectedPicker!,
                                      urgent: urgentWhere, important: importantWhere,
                                      )));
                          }
                      ),
@override
  void initState() {
    super.initState();
    myQuery(time,energy,urgent,important).then((results) {
      setState(() {
        querySnapshot = results;
      });
    });

 queryEngage(time,energy,urgent,important).then((results) {
      setState(() {
        querySnapshot = results;
      });
    });  
  }
 Future queryEngage (time,energy,urgent,important) async {
    await myQueryV2();
    await myQueryV3 (statusQuery,time);
    await myQueryV4 (timeQuery,energy);
    await myQueryV5 (energyQuery,urgent);
    await myQueryV6 (urgentQuery,important);
  }
Future myQueryV2 ( ) async{
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';
    final currentQuery = FirebaseFirestore.instance.collection(path);

     statusQuery = currentQuery.where('status', isEqualTo: 'Inbox');
     return statusQuery;
  }

  Future myQueryV3 (statusQuery, time) async {

     timeQuery = statusQuery.where('time_Needed', isEqualTo: time);

     return timeQuery;
  }

  Future myQueryV4 (timeQuery, energy) async {
     energyQuery = timeQuery.where('energy_Needed', isEqualTo: energy);

     return energyQuery;
  }

  Future myQueryV5 (energyQuery, urgent) async {

     urgentQuery = energyQuery.where('urgent', isEqualTo: urgent);
     return urgentQuery;
  }

  Future myQueryV6 (urgentQuery, important) async {

     importantQuery = urgentQuery.where('important', isEqualTo: important);

    print ('urgent: $urgent');
    print ('important: $important');
    print ('time: $time');
    print ('energy: $energy');

    final snapshot = await importantQuery.get();
    final data = snapshot.docs;

    if(data.isNotEmpty) {
      return snapshot;
    }
  }

CodePudding user response:

You could write a single compound query like this. It is not necessary to get the results at each stage based on the code shown. You may need to create some composite indexes to improve optimization, but Firebase should notify you automatically if this is the case.

Future myQuery (  time,  energy,  urgent,  important ) async{
  final uid = FirebaseAuth.instance.currentUser!.uid;
  final path = 'Users/$uid/allTasks';
  final currentQuery = FirebaseFirestore.instance.collection(path);

 try {
    final snapshot = currentQuery
        .where('status', isEqualTo: 'Inbox')
        .where('time_Needed', isEqualTo: time)
        .where('energy_Needed', isEqualTo: energy)
        .where('urgent', isEqualTo: urgent)
        .where('important', isEqualTo: important);

    await snapshot.get().then((data) {
      for (var doc in data.docs) {
        print("${doc.id} => ${doc.data()}");
      }
    });
  } catch (err) {
    print(err);
  }
}
  • Related