在我更新Flutter的最新版本(见下文)之前,以下代码提取用于工作,现在我收到异常。
这段代码的目的是调用一个Future,它将返回一个AJAX调用到我服务器的答案。
使用新版本的Flutter(0.2.8),使用Dart 2.0.0,似乎未来< T>不能再回到另一个未来<字符串> (另一种类型)[见下面的代码]。
我找不到答案。有谁可以帮助我?
非常感谢
Flutter版本:
flutter --version
Flutter 0.2.8 • channel beta • https://github.com/flutter/flutter.git
Framework • revision b397406561 (12 days ago) • 2018-04-02 13:53:20 -0700
Engine • revision c903c217a1
Tools • Dart 2.0.0-dev.43.0.flutter-52afcba357
代码提取:
///
/// Returns the list of items belonging to the active profile
///
Future<PageAnswer> getItems({
int pageIndex: 0,
int pageSize: 50,
@required String sortOrder
}) async {
String url = "ItemsList/$sortOrder";
return ajaxGet(url).then((String responseBody) async {
final Map response = json.decode(responseBody);
final _status = response["status"];
if (_status == "OK"){
// Everything is OK
Map map = json.decode(response["data"]);
List<Map> objects = map["List"];
int total = map["Total"];
return new PageAnswer(objects, total);
}
return Null;
}).catchError((){
return Null;
});
}
Http获取实施:
/// ----------------------------------------------------------
/// Http "GET" request
/// ----------------------------------------------------------
Future<String> ajaxGet(String serviceName) async {
var responseBody = '{"data": "", "status": "NOK"}';
try {
var httpClient = new HttpClient();
var uri = new Uri.http(mid.serverUrl, _serverApi + serviceName);
var request = await httpClient.getUrl(uri);
request.headers
..add('X-mobile-uuid', await _getDeviceIdentity())
..add('X-mobile-token', await mid.getMobileToken());
var response = await request.close();
if (response.statusCode == 200){
responseBody = await response.transform(UTF8.decoder).join();
}
} catch(e){
// An error was received
print("AJAX ERROR");
}
return responseBody;
}
代码调用:
PageAnswer answer = await getItems(sortOrder: 'alpha');
...
类型:
typedef Future<PageAnswer> ApiPageRequest(int page, int pageSize);
class PageAnswer {
final List<Map> list;
final int total;
PageAnswer(this.list, this.total);
}
异常堆栈:
E/flutter ( 6926): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 6926): type 'Future<dynamic>' is not a subtype of type 'FutureOr<PageAnswer>' where
E/flutter ( 6926): Future is from dart:async
E/flutter ( 6926): FutureOr is from dart:async
E/flutter ( 6926): PageAnswer is from file:///D:/Development/myapp/lib/libraries/function_types.dart
E/flutter ( 6926):
E/flutter ( 6926): #0 MIDApi.getItems (file:///D:/Development/myapp/lib/libraries/api.dart:39:22)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #1 _LandingPageState.doSomething (file:///D:/Development/myapp/lib/pages/landing.dart:129:39)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #2 _LandingPageState._handleTap (file:///D:/Development/myapp/lib/pages/landing.dart:124:7)
E/flutter ( 6926): #3 _LandingPageState.build.<anonymous closure>.<anonymous closure> (file:///D:/Development/myapp/lib/pages/landing.dart:61:26)
E/flutter ( 6926): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 6926): #5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter ( 6926): #6 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
E/flutter ( 6926): #7 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 6926): #8 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter ( 6926): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 6926): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 6926): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 6926): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 6926): #13 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter ( 6926): #14 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
E/flutter ( 6926): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 6926): type '() => Null' is not a subtype of type '(Object) => FutureOr<Null>'
E/flutter ( 6926): #0 _FutureListener.handleError (dart:async/future_impl.dart:145:11)
E/flutter ( 6926): #1 Future._propagateToListeners.handleError (dart:async/future_impl.dart:645:47)
E/flutter ( 6926): #2 Future._propagateToListeners (dart:async/future_impl.dart:666:24)
E/flutter ( 6926): #3 Future._completeError (dart:async/future_impl.dart:485:5)
E/flutter ( 6926): #4 _SyncCompleter._completeError (dart:async/future_impl.dart:55:12)
E/flutter ( 6926): #5 _Completer.completeError (dart:async/future_impl.dart:27:5)
E/flutter ( 6926): #6 MIDApi.getLooks.<anonymous closure> (file:///D:/Development/myapp/lib/libraries/api.dart)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #7 _RootZone.runUnary (dart:async/zone.dart:1381:54)
E/flutter ( 6926): #8 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 6926): #9 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633:45)
E/flutter ( 6926): #10 Future._propagateToListeners (dart:async/future_impl.dart:662:32)
E/flutter ( 6926): #11 Future._complete (dart:async/future_impl.dart:467:7)
E/flutter ( 6926): #12 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 6926): #13 ajaxGet (file:///D:/Development/myapp/lib/libraries/ajax.dart)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #14 MIDApi.getItems (file:///D:/Development/myapp/lib/libraries/api.dart:22:5)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #15 _LandingPageState.doSomething (file:///D:/Development/myapp/lib/pages/landing.dart:129:39)
E/flutter ( 6926): <asynchronous suspension>
E/flutter ( 6926): #16 _LandingPageState._handleTap (file:///D:/Development/myapp/lib/pages/landing.dart:124:7)
E/flutter ( 6926): #17 _LandingPageState.build.<anonymous closure>.<anonymous closure> (file:///D:/Development/myapp/lib/pages/landing.dart:61:26)
E/flutter ( 6926): #18 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 6926): #19 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter ( 6926): #20 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
E/flutter ( 6926): #21 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 6926): #22 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter ( 6926): #23 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 6926): #24 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 6926): #25 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 6926): #26 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 6926): #27 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter ( 6926): #28 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
Reloaded 15 of 522 libraries in 634ms.