I’m trying to pass the data of a List for another screen but i don’t know how to do that.
I’ve tried using constructors and using correctly null-safety but the app returned this error: Null check operator used on a null Value. And i’m a long time already trying to solve this problem but i don’t know how. What’s the benefits of null safety? Here’s the code:
Code 1
import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:mood3/telas/RecebeDados.dart';
import 'package:mood3/telas/listas.dart';
import 'package:path_provider/path_provider.dart';
import 'package:toast/toast.dart';
import 'Diary.dart';
class Escrita extends StatefulWidget {
List? lista = [];
Escrita({this.lista});
@override
State<Escrita> createState() => _EscritaState();
}
class _EscritaState extends State<Escrita> {
Future<File>_getFile()async{
final diretorio = await getApplicationDocumentsDirectory();
return File("${diretorio.path}/dados.json");
}
_salvar() async {
var arquivo = await _getFile();
Map<String,dynamic> tarefa = Map();
tarefa["titulo"] = "Ir ao mercado";
tarefa["realizada"] = false;
widget.lista!.add(tarefa);
String dados = json.encode(widget.lista);
arquivo.writeAsString(dados);
}
_ler() async {
try{
final arquivo = await _getFile();
return arquivo.readAsString();
}catch(e){
return null;
}
}
@override
void initState() {
super.initState();
_ler().then((dados){
setState(() {
widget.lista = json.decode(dados);
});
});
}
TextEditingController _controllerTarefa = TextEditingController();
@override
Widget build(BuildContext context) {
ToastContext().init(context);
return Scaffold(
body: Center(
child: SingleChildScrollView(
child:Column(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
child: TextField(
keyboardType: TextInputType.multiline,
controller: _controllerTarefa,
decoration: InputDecoration(
hintText: "Diary Title",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.blueGrey,
),
)),
Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16),
child: TextField(
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
hintText: "Diary Text",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.blueGrey,
),
),),
],
),),
),
}
),
]
),
);
}
}
Code 2:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:mood3/telas/Account.dart';
import 'package:mood3/telas/Diary.dart';
import 'package:mood3/telas/Friends.dart';
import 'package:mood3/telas/Graph.dart';
import 'package:mood3/telas/Home.dart';
import 'package:mood3/telas/Music.dart';
import 'package:mood3/telas/PlayerDoVideo.dart';
import 'package:mood3/telas/Videos.dart';
import 'package:mood3/telas/animacao.dart';
import 'package:mood3/telas/escrita.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent-tab-view.dart';
class RecebeDados extends StatefulWidget {
@override
State<RecebeDados> createState() => _RecebeDadosState();
}
class _RecebeDadosState extends State<RecebeDados> {
Escrita flista = Escrita();
late List? list = flista.lista;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: list!.length,
itemBuilder: (context, index){
return ListTile(
title: Text( list![index]['titulo'] ),
);
}
),
)
],
),
floatingActionButton: SpeedDial(
animatedIcon: AnimatedIcons.menu_close,
backgroundColor: Colors.black,
icon: CupertinoIcons.pencil,
overlayColor: Colors.black,
children: [
Text ("teste")
]
),
);
}
}
2
Answers
In this case you are creating "lista" as a List that could be null (List?), but also you initialize it with value: a empty list ( [] )
But when you call the constructor of Escrita, you are giving a value to the lista variable.
lista is null, because you are not indicating a value in the constructor.
If you want a default value this is the correct way:
You are receiving errors because you are using list!.length and list![index][‘titulo’], and using the ! you are asecuring that the list is not null, but yes, its null! So thats the reason because you are receiving the error.
You can solve your problem in this way:
Or you can do this:
Or