Confusing about Future Object in Dart –

Issue

Any idea what is wrong here?

I have a function which receive data from server by a rest call, which return Future object.

Future _function(){
 var future = _getWithID('path');
  future.then((data) {
    List<SOMEOBJECT> SOMEOBJECT = data["entities"].map((v) {
          return new SOMEOBJECT(
              id: v["properties"]["id"],
              name: titleize(v["properties"]["name"])
          );
        }).toList();
    return new Future.value(SOMEOBJECT.process(SOMEOBJECT));
  });
}

Model class for SOMEOBJECT

class SOMEOBJECT extends Model {

  int id;

  String name;

  SOMEOBJECT({this.id, this.name});

  static Map process(List<String> SOMEOBJECTS) {
    // map = {name: A, value:[{name:list<String>},{name:list<String>}]}
    return map;
  }
}

Cache object which try to cache in browser

class CacheManager {
  Map callbacks;
  Map cache;

  CacheManager(this.cache){
    callbacks = {};
  }

  Future setData(String key, Function updateFunction) {
    return chrome.storage.local.get(key).then( (resp){
      cache[key] = resp[key];

      return updateFunction().then((data) {
        chrome.storage.local.set({key: data});
        cache[key] = data;
      }).whenComplete( () {
        handleCallbacks(key);
      });
    });
  }

  void registerCallback(String key, Function callback) {
    callbacks[key] = callback;
  }

  void handleCallbacks(String key){
    if (callbacks[key] != null){
        callbacks[key](cache[key]);
    }
  }
}

So I have these two lines before

cacheManager.registerCallback("SOMEOBJECT", loadSomeOBJECT);
cacheManager.setData('SOMEOBJECT', api._function);

and I am getting this error:

ERROR
NoSuchMethodError: method not found: 'then'
Receiver: null
Arguments: [Closure: (dynamic) => dynamic]
#0      Object._noSuchMethod (dart:core-patch/object_patch.dart:42)
#1      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#2      CacheManager.setData.<anonymous closure> (chrome-extension://ekfcndmmkincdeoolhcebmhcgmkmadip/helpers.dart:27:31)
#3      _RootZone.runUnary (dart:async/zone.dart:1149)
#4      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:551)
#5      _Future._propagateToListeners (dart:async/future_impl.dart:637)
#6      _Future._completeWithValue (dart:async/future_impl.dart:424)
#7      _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:479)
#8      _microtaskLoop (dart:async/schedule_microtask.dart:41)
#9      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#10     _ScheduleImmediateHelper._handleMutation (dart:html:49254)
#11     MutationObserver._create.<anonymous closure> (dart:html:27525)‚Äč

this refer to the line which setData and callback from cache object. then the cache object will call the api.function to get the data from server, and then the raw data is served, it goes to process method in SOMEOBJECT class and return the MAP of JSON representation. Once the data back to cache manager to call the then on the future object it fails. with the error on the question. Any idea?
Thanks

Solution

I just had a brief look and saw

Future _function(){
 var future = _getWithID('path');
  return future.then((data) {

is missing a return. There might be other issues though.

Answered By – Günter Zöchbauer

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *