vendredi 19 juin 2020

I want to make a checkbox. A non-null String must be provided to a Text widget

I want to make a checkbox. This is an error that I get when running my code:

A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 285 pos 10: 'data != null'

Here is my code:

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'datagejala.dart';

class Diagnosis2 extends StatefulWidget {
  @override
  _Diagnosis2State createState() => _Diagnosis2State();
}

class _Diagnosis2State extends State<Diagnosis2> {
  //initState
  bool selected = false;
  var userStatus = List<bool>();

  Future<List<DataGejala>> getContactsFromJSON(BuildContext context) async{
    String jsonString = await DefaultAssetBundle.of(context).loadString("assets/data/penyakit.json");
    List<dynamic> raw = jsonDecode(jsonString);
    return raw.map((f) => DataGejala.fromJSON(f)).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Select City'),
      ),
      body: Container(
        child: FutureBuilder(
          future: getContactsFromJSON(context),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            print(snapshot.data);
            if (snapshot.data == null) {
              return Container(child: Center(child: Text("Loading...")));
            } else {
              return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(snapshot.data[index].g_nama),
                    trailing: Checkbox(
                        value: userStatus[index],
                        onChanged: (bool val) {
                          setState(() {
                            userStatus[index] = !userStatus[index];
                          });
                        }),
                  );
                },
              );
            }
          },
        ),
      ),
    );
  }
}



Aucun commentaire:

Enregistrer un commentaire