Flutter 调用模型的工厂构造函数时的省道/颤振问题

Flutter 调用模型的工厂构造函数时的省道/颤振问题,flutter,mobile,dart,provider,Flutter,Mobile,Dart,Provider,我有一个Inbox的模型类,它包含一个普通构造函数和一个接受列表的工厂构造函数。一切似乎都正常,除了调用工厂构造函数之后,代码不会继续到下一行_收件箱=收件箱.fromList(_fetchInboxResBody['inbox']) 下面是我调用收件箱的工厂构造函数的代码 import 'package:flutter/foundation.dart'; import 'dart:async'; import 'package:http/http.dart' as http; import '

我有一个Inbox的模型类,它包含一个普通构造函数和一个接受列表的工厂构造函数。一切似乎都正常,除了调用工厂构造函数之后,代码不会继续到下一行_收件箱=收件箱.fromList(_fetchInboxResBody['inbox'])

下面是我调用收件箱的工厂构造函数的代码

import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

import '../models/inbox.dart';

class InboxProvider with ChangeNotifier {
  final _rootUrl = 'http://localhost:8080';
  // final _rootUrl = 'http://firstcarestartup.appspot.com';

  //inbox as an array
  Map<String, dynamic> _fetchInboxResBody;
  Map<String, dynamic> _fetchInboxResError;
  Inbox _inbox;
  //getters
  Inbox get inbox => _inbox;
  Map<String, dynamic> get fetchInboxResError => _fetchInboxResError;

  fetchInbox(String accessToken) async {
    print('fetching inbox');
    try {
      var response = await http.get('$_rootUrl/customer/inbox', headers: {
        'Content-Type': 'application/json',
        'Authorization': 'bearer $accessToken'
      });
      _fetchInboxResBody = json.decode(response.body);
      if (_fetchInboxResBody.containsKey('error')) {
        _fetchInboxResError = _fetchInboxResBody;
        notifyListeners();
      } else {
        _fetchInboxResError = null;
        print('yeah1');
        _inbox = Inbox.fromList(_fetchInboxResBody['inbox']);
        print('yeah');
        notifyListeners();
      }
    } catch (e) {
      _fetchInboxResError = {'error': e.toString()};
      notifyListeners();
    }
  }
}
导入“包:flift/foundation.dart”;
导入“dart:async”;
将“package:http/http.dart”导入为http;
导入“dart:convert”;
导入“../models/inbox.dart”;
使用ChangeNotifier初始化InboxProvider{
最终的http://localhost:8080';
//最终的http://firstcarestartup.appspot.com';
//收件箱作为数组
映射_fetchInboxResBody;
映射_fetchInboxResError;
收件箱(Inbox);;
//吸气剂
收件箱获取收件箱=>\u收件箱;
映射get-fetchInboxResError=>\u-fetchInboxResError;
fetchInbox(字符串accessToken)异步{
打印(“提取收件箱”);
试一试{
var response=wait http.get(“$\u rootUrl/customer/inbox”,标题:{
“内容类型”:“应用程序/json”,
“授权”:“持有人$accessToken”
});
_fetchInboxResBody=json.decode(response.body);
if(_fetchInboxResBody.containsKey('error')){
_fetchInboxResError=\u fetchInboxResBody;
notifyListeners();
}否则{
_fetchInboxResError=null;
打印('yeah1');
_收件箱=收件箱.fromList(_fetchInboxResBody['inbox']);
打印(‘是’);
notifyListeners();
}
}捕获(e){
_fetchInboxResError={'error':e.toString()};
notifyListeners();
}
}
}
这是inbox.dart

import 'package:flutter/material.dart';

class Inbox {
  List<Message> messages;
  int unread;

  Inbox({@required this.messages, @required this.unread});

  factory Inbox.fromList(List response) {
    int counter = 0;
    List msgs = new List();
    response.forEach((f) {
      if(f['read'] == false){
        counter++;
      }
      Message msg = Message.fromMap(f);
      msgs.add(msg);
    });
    print(msgs[2].content);
    return new Inbox(messages: msgs, unread: counter);
  }
}

class Message {
  String content;
  String date;
  String header;
  String id;
  String imageUrl;
  bool read;
  String type;

  Message(
      {@required this.content,
      @required this.date,
      @required this.header,
      @required this.id,
      this.imageUrl,
      @required this.read,
      this.type});

  factory Message.fromMap(Map messageMap) {

    var content = messageMap['content'];
    var date = messageMap['date'];
    var header = messageMap['header'];
    var id = messageMap['id'];
    var imageUrl = messageMap['image'] ?? null;
    var read = messageMap['read'];
    var type = messageMap['type'];

    return new Message(content: content, date: date, header: header, id: id, read: read, imageUrl: imageUrl, type: type);
  }
}
导入“包装:颤振/材料.省道”;
类收件箱{
列出信息;
int未读;
收件箱({@required this.messages,@required this.unread});
工厂收件箱.fromList(列表响应){
int计数器=0;
List msgs=新列表();
答复.forEach((f){
如果(f['read']==false){
计数器++;
}
Message msg=Message.fromMap(f);
添加(味精);
});
打印(msgs[2]。内容);
返回新收件箱(邮件:msgs,未读:计数器);
}
}
类消息{
字符串内容;
字符串日期;
字符串头;
字符串id;
字符串imageUrl;
布尔-里德;
字符串类型;
信息(
{@required this.content,
@此日期为,
@需要此.header,
@需要这个.id,
这个.imageUrl,
@需要这个。阅读,
该类型});
工厂消息.fromMap(映射消息映射){
var content=messageMap['content'];
var date=messageMap['date'];
var header=messageMap['header'];
var id=messageMap['id'];
var imageUrl=messageMap['image']??空;
var read=messageMap['read'];
var type=messageMap['type'];
返回新消息(内容:content,日期:date,header:header,id:id,read:read,imageUrl:imageUrl,type:type);
}
}

ps.终端打印的是“yeah1”,但不是“yeah1”,可能是工厂构造函数抛出了一个异常,我看到您正在捕获该异常。尝试在catch中打印异常。