错误:flutter/lib/ui/ui_dart_state.cc(186)

时间:2021-05-18 05:59:59

标签: flutter dart

我遇到的错误

<块引用>

E/flutter (11662): [错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:
NoSuchMethodError:在 null 上调用了 getter 'data'。
E/flutter (11662):接收器:null
E/flutter (11662):尝试调用:data
E/flutter (11662): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (11662): #1 _GridHomeState._getGridNews.. (package:indonesia_bisa/Widget/Home/GridView.dart:23:30)
E/flutter (11662):#2 State.setState (包:flutter/src/widgets/framework.dart:1267:30)
E/flutter (11662):#3 _GridHomeState._getGridNews。 (包:indonesia_bisa/Widget/Home/GridView.dart:22:7)
E/flutter (11662): #4 _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (11662): #5 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (11662): #6 _FutureListener.handleValue (dart:async/future_impl.dart:152:18)
E/flutter (11662): #7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:704:45)
E/flutter (11662): #8 Future._propagateToListeners (dart:async/future_impl.dart:733:32)
E/flutter (11662): #9 Future._completeWithValue (dart:async/future_impl.dart:539:5)
E/flutter (11662): #10 _completeOnAsyncReturn (dart:async-patch/async_patch.dart:254:13)
E/flutter (11662): #11 Api.getGridNews (package:indonesia_bisa/API/API.dart)
E/颤振(11662):<asynchronous suspension> E/扑(11662):

Dart 到 Json

class Sorotan {
  String _rc;
  String _pesan;
  List<Data> _data;

  Sorotan({String rc, String pesan, List<Data> data}) {
    this._rc = rc;
    this._pesan = pesan;
    this._data = data;
  }

  String get rc => _rc;
  set rc(String rc) => _rc = rc;
  String get pesan => _pesan;
  set pesan(String pesan) => _pesan = pesan;
  List<Data> get data => _data;
  set data(List<Data> data) => _data = data;

  Sorotan.fromJson(Map<String, dynamic> json) {
    _rc = json['rc'];
    _pesan = json['pesan'];
    if (json['data'] != null) {
      _data = new List<Data>();
      json['data'].forEach((v) {
        _data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['rc'] = this._rc;
    data['pesan'] = this._pesan;
    if (this._data != null) {
      data['data'] = this._data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Data {
  String _idBerita;
  String _idKategori;
  String _namaMasjid;
  String _namaKategori;
  String _gambar;
  String _judul;
  String _isi;
  String _penulis;
  String _waktu;
  String _status;

  Data(
      {String idBerita,
        String idKategori,
        String namaMasjid,
        String namaKategori,
        String gambar,
        String judul,
        String isi,
        String penulis,
        String waktu,
        String status}) {
    this._idBerita = idBerita;
    this._idKategori = idKategori;
    this._namaMasjid = namaMasjid;
    this._namaKategori = namaKategori;
    this._gambar = gambar;
    this._judul = judul;
    this._isi = isi;
    this._penulis = penulis;
    this._waktu = waktu;
    this._status = status;
  }

  String get idBerita => _idBerita;
  set idBerita(String idBerita) => _idBerita = idBerita;
  String get idKategori => _idKategori;
  set idKategori(String idKategori) => _idKategori = idKategori;
  String get namaMasjid => _namaMasjid;
  set namaMasjid(String namaMasjid) => _namaMasjid = namaMasjid;
  String get namaKategori => _namaKategori;
  set namaKategori(String namaKategori) => _namaKategori = namaKategori;
  String get gambar => _gambar;
  set gambar(String gambar) => _gambar = gambar;
  String get judul => _judul;
  set judul(String judul) => _judul = judul;
  String get isi => _isi;
  set isi(String isi) => _isi = isi;
  String get penulis => _penulis;
  set penulis(String penulis) => _penulis = penulis;
  String get waktu => _waktu;
  set waktu(String waktu) => _waktu = waktu;
  String get status => _status;
  set status(String status) => _status = status;

  Data.fromJson(Map<String, dynamic> json) {
    _idBerita = json['id_berita'];
    _idKategori = json['id_kategori'];
    _namaMasjid = json['nama_masjid'];
    _namaKategori = json['nama_kategori'];
    _gambar = json['gambar'];
    _judul = json['judul'];
    _isi = json['isi'];
    _penulis = json['penulis'];
    _waktu = json['waktu'];
    _status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id_berita'] = this._idBerita;
    data['id_kategori'] = this._idKategori;
    data['nama_masjid'] = this._namaMasjid;
    data['nama_kategori'] = this._namaKategori;
    data['gambar'] = this._gambar;
    data['judul'] = this._judul;
    data['isi'] = this._isi;
    data['penulis'] = this._penulis;
    data['waktu'] = this._waktu;
    data['status'] = this._status;
    return data;
  }
}

在哪里使用

class GridHome extends StatefulWidget {
  @override
  _GridHomeState createState() => _GridHomeState();
}

class _GridHomeState extends State<GridHome> {

  var listGrid = List<Sorotan>();
  var data = List<Data>();

  void _getGridNews() async{
    Api.getGridNews().then((response){

      //perubahan kondisi sesuaidengan jumlah response yang diterima dari webservice
      setState(() {
        this.data = response.data;
      });

    });
  }

  @override
  void initState() {
    super.initState();
    _getGridNews();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 62.0.h),
      height: 40.h,
      width: 100.w,
      child: Padding(
        padding: EdgeInsets.only(right: 1.5.h,left: 1.5.h,bottom: 16.0.h),
        child: GridView.builder(
          itemCount: data.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2),
          itemBuilder:(BuildContext context, int index) {
            return GestureDetector(
              onTap: (){
                //  menampilkan halaman detail
                Navigator.push(context, MaterialPageRoute(builder: (context) => DetailNewsScreen(
                )));
              },
              child: Card(
                elevation: 10,
                color: Colors.grey[200],
                child: Container(
                  child: Image.network(
                        *data[index].gambar*,
                        fit: BoxFit.cover,
                        ),
                ),
              ),
            );
            },
        ),
      ),
    );
  }
}

我已经导入了所有需要的东西, 我想把图片放在Gridview上

API 请求

static Future<Sorotan> getGridNews() async {
    var request = http.Request(
        'GET',
        Uri.parse(
            '-----'));
    http.StreamedResponse response = await request.send();
    if (response.statusCode == 200) {
      print(await response.stream.bytesToString());
    } else {
      print(response.reasonPhrase);
    }
  }

请帮我解决这个错误

0 个答案:

没有答案