在 null Flutter 上调用了 getter 'length'

时间:2021-05-22 13:40:56

标签: android json flutter dart

我正在尝试从我的本地 JSON 文件中获取数据并显示它。一切都准备好了,但我得到了 The getter 'length' was called on null error.它在另一个模拟器中工作,但是当我改变它时它停止工作。当我删除length方法时getter[]被调用为null。

我在网上查了一下,但找不到适合我的东西。谁能告诉我我做错了什么? 这是我的代码:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:medicine_reminder/src/ui/homepage/homepage.dart';

class JsonPage extends StatefulWidget {
  @override
  _JsonPageState createState() => _JsonPageState();
}

class _JsonPageState extends State<JsonPage> {
  List data;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 90,
          backgroundColor: Color(0xFF3EB16F),
          title: Text("Medicine List"),
          centerTitle: true,
          textTheme: TextTheme(
            headline6: TextStyle(fontSize: 44, fontFamily: "Angel"),
          ),
        ),
        body: new Container(
          child: new Center(
              child: new FutureBuilder(
            future: DefaultAssetBundle.of(context)
                .loadString('assets/csvjson_2son.json'),
            builder: (context, snapshot) {
              var mydata = JsonDecoder().convert(snapshot.data.toString());

              data.length;
              return new ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                _listItem(index);
                return new Card(
                    child: new Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    new Text(
                      "" + mydata[index]['ILAC ADI'],
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.deepOrange),
                    ),
                    new Text("Etken Madde: " + mydata[index]['ETKIN MADDE']),
                    new Text("ATC Adı: " + mydata[index]['ATC ADI']),
                    new Text("ATC Kodu: " + mydata[index]['ATC KODU']),
                    new Text("Barkod: " + mydata[index]['BARKOD'].toString()),
                    new Text("Firma Adı: " + mydata[index]['FIRMA ADI']),
                    new Text("Referans Eşdeğeri: " +
                        mydata[index]['REFERANS \nESDEGER']),
                    new Text(
                        "Eşdeğeri: " + mydata[index]['ESDEGERI'].toString()),
                    new Text("Ambalaj Miktarı: " +
                        mydata[index]['AMBALAJ MIKTARI'].toString()),
                    new Text("Reçete: " + mydata[index]['RECETE']),
                    new Text("Satış Fiyatı: " +
                        mydata[index]['KDV DAHIL PERAKENDE SATIS TL FIYATI']
                            .toString()),
                  ],
                ));
              });
            },
          )),
        ),
        floatingActionButton: Stack(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(left: 15),
              child: Align(
                heightFactor: 13.6,
                alignment: Alignment.topLeft,
                child: FloatingActionButton(
                  backgroundColor: Colors.transparent,
                  onPressed: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => HomePage(),
                        ));
                  },
                  child: Icon(Icons.arrow_back),
                ),
              ),
            ),
          ],
        ));
  }

  _listItem(index) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.only(
            top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              data[index].title,
              style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
            ),
            Text(
              data[index].text,
              style: TextStyle(color: Colors.grey.shade600),
            )
          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案