每当我进入屏幕时,SuchMethodError不会抖动

时间:2020-06-22 22:17:12

标签: flutter dart

我是新手,可能我的代码也不完美。当我连接到屏幕时,出现以下错误:方法'[]'在null上调用。 接收者:null 尝试调用:[](“ 1”)

有人有想法吗? 附加代码。 谢谢。

'class Shibutz extends StatefulWidget {
  @override
  _ShibutzState createState() => _ShibutzState();
}

class _ShibutzState extends State<Shibutz> {

  Map data;
  List userData;

  Future getData() async {
    http.Response response = await http.get(
        Uri.encodeFull("My-Link-To-Json"),
        headers: {
          "Accept": "application/json"
        }

    );
    String body = utf8.decode(response.bodyBytes);
    data = json.decode(body);


  }
  @override
  void initState() {
    setState(() {
      getData();

    });

  }

  Widget build(BuildContext context) {
    var regex = new RegExp(r'[a-zA-Z?=.*[!@#$%^&*£()"+-_0-9]');
    String Ramash1 = data["1"]["data"].replaceAll(regex, ' ');
    String T2 = data["2"]["data"].replaceAll(regex, ' ');
    String T3 = data["3"]["data"].replaceAll(regex, ' ');
    String T4 = data["4"]["data"].replaceAll(regex, ' ');
    String T5 = data["5"]["data"].replaceAll(regex, ' ');
    String T6 = data["6"]["data"].replaceAll(regex, ' ');
    String T7 = data["7"]["data"].replaceAll(regex, ' ');
    String T8 = data["8"]["data"].replaceAll(regex, ' ');

    List<VehicleTeam> teams = [
      VehicleTeam(fire: 'תעופה 1', logo: '1.png' , fighter1: '${Ramash1}' , fighter2:''),
      VehicleTeam(fire: 'תעופה 2', logo: '2.png' , fighter1: '${T2}' , fighter2:' ' , fighter3: ' ' ),
      VehicleTeam(fire: 'תעופה 3', logo: '3.png' , fighter1: '${T3}' , fighter2:'' , fighter3: '' ),
      VehicleTeam(fire: 'תעופה 4', logo: '4.png' , fighter1: '${T4}' , fighter2:'' , fighter3: '' ),
      VehicleTeam(fire: 'תעופה 5', logo: '5.png' , fighter1: '${T5}' , fighter2:'' , fighter3: '' ),
      VehicleTeam(fire: 'אמבולנס', logo: 'ambu.png' , fighter1: '${T8}' , fighter2:'' , fighter3: '' ),
      VehicleTeam(fire: 'יומנאי', logo: 'cisco.png' , fighter1: '${T7}' , fighter2:'' , fighter3: '' ),
      VehicleTeam(fire: 'כיבוי אש עירוני', logo: 'Fire102.png' , fighter1: '${T6}' , fighter2:''  ),

    ];'

例如,我的json文件是:

{"1":{"id":"1","data":"[\"לטקה\\\/כפיר\"]"},"2":{"id":"2","data":"[\"צרפתי\",\"טל\",\"מוטי\"]"},"3":{"id":"3","data":"[\"נווה\",\"רותם\",\"אנדריי\"]"},"4":{"id":"4","data":"[\"נמרוד\",\"דימה\",\"\"]"},"5":{"id":"5","data":"[\"\",\"--לא משובץ--\",\"\"]"},"6":{"id":"6","data":"[\"אדמונד 053-7397878\",\"רן\"]"},"7":{"id":"7","data":"[\"שרון\"]"},"8":{"id":"8","data":"[\"מוטי\"]"}}

2 个答案:

答案 0 :(得分:0)

如果尚未提取数据,则需要显示备用内容。因此,检查data是否等于null(表示尚未从服务器获取)是否显示后备内容,例如:Text('Loading...')

class Shibutz extends StatefulWidget {
  @override
  _ShibutzState createState() => _ShibutzState();
}

class _ShibutzState extends State<Shibutz> {
  Map data;
  List userData;

  Future getData() async {
    http.Response response = await http.get(
        Uri.encodeFull("My-Link-To-Json"),
        headers: {
          "Accept": "application/json"
        }
    );

    String body = utf8.decode(response.bodyBytes);
    
    // Instead set state after the data were ready to display.
    setState(() {
       data = json.decode(body);
    });
  }

  @override
  void initState() {
    // No need to setState here
    getData();
  }

  Widget build(BuildContext context) {
    // Return something else if data is still loading (not available).
    if (data == null) {
      return Text('Loading...');
    }

    // Otherwise display content
    ...
  }
}

答案 1 :(得分:0)

尝试更改声明变量的方式

var data = new Map();
List userData = [];

就我而言,它工作正常。