I was follwing a video to implement listview builder but got an error mentioned above, I think its due to null safety but don't know how to solve it.I am working on vscode and Launching lib\main.dart on Chrome in debug mode.Following is my complete code:
There are two classes i.e. main.dart and note.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:listview/entities/note.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
await http.get(Uri.parse("https://jsonplaceholder.typicode.com/posts"));
var notes = <Note>[];
if (response.statusCode == 200) {
var notesJson = jsonDecode(response.body);
for (var noteJson in notesJson) {
notes.add(Note.fromJson(noteJson));
}
}
return notes;
}
@override
Widget build(BuildContext context) {
fetchNotes().then((value) {
_notes.addAll(value);
});
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Listview with Json"),
),
body: ListView.builder(
itemBuilder: (context, 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(
_notes[index].title,
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold),
),
Text(
_notes[index].text,
style: TextStyle(color: Colors.grey.shade700),
)
],
),
),
);
},
itemCount: _notes.length,
)));
}
}
class Note {
late String title;
late String text;
Note(this.title, this.text);
Note.fromJson(Map<String, dynamic> json) {
title = json['title'];
text = json['text'];
}
}