Home > Net >  Future List value is empty but it wasn't empty before
Future List value is empty but it wasn't empty before

Time:08-28

I released an app yesterday, and I just found out today that it had a bug where the future would return an empty list.

My goal is to return which category the user is (the two categories are User and Admin).

This is the code for the future list.

Future<List> retrieveuserinfo() async{
    List x = [];
    await FirebaseDatabase.instance.ref().get().then((value) {
      for(var i in value.children){
        for(var j in i.children){
          if(j.key.toString() == FirebaseAuth.instance.currentUser?.uid.toString()){
            x.add(i.key);
          }
        }
      }
    });
    print(x);
    return x;
  }

The only problem is that it worked before but it doesn't work now.

This is the error I got.

The following RangeError was thrown building FutureBuilder<List<dynamic>>(dirty, state: _FutureBuilderState<List<dynamic>>#26c7c):
RangeError (index): Invalid value: Valid value range is empty: 0

The relevant error-causing widget was: 
  FutureBuilder<List<dynamic>> FutureBuilder:file:///C:/Users/ammar/AndroidStudioProjects/ttpflutter/lib/home.dart:155:93
When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:281:36)
#1      _homeState.build.<anonymous closure> (package:ttpflutter/home.dart:211:60)
#2      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:615:55)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#5      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#7      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#8      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#9      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#11     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#12     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
#16     _invoke (dart:ui/hooks.dart:151:10)
#17     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#18     _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)

Is there any way to fix this.

CodePudding user response:

Don't combine await and then. Use one or the other, but not both.

Future<List> retrieveuserinfo() async{
  var value = await FirebaseDatabase.instance.ref().get();
  List x = [];
  for(var i in value.children){
    for(var j in i.children){
      if(j.key.toString() == FirebaseAuth.instance.currentUser?.uid.toString()){
        x.add(i.key);
      }
    }
  }
  print(x);
  return x;
}
  • Related