I am trying to get the data from an API and I get the data, but when I show it on the screen I get this error
Assertion failed: file:///home/builder/hotbuilder/packages/flutter/lib/src/widgets/text.dart:378:10 data != null.
I saw some examples, but I still don't understand how to solve it
This is main.dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_api_git/modelo/repo.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: Home(),
);
}
}
Future<All> fetchRepos() async {
final response =
await http.get(Uri.parse('https://api.github.com/users/IcaroOli/repos'));
if (response.statusCode == 200) {
print(response.body);
return All.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to fetch repos!');
}
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
late Future<All> futureRepo;
@override
void initState() {
super.initState();
futureRepo = fetchRepos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('GitHub API!'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: FutureBuilder<All>(
future: futureRepo,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Repo> repos = <Repo>[];
for (int i = 0; i < snapshot.data!.repos.length; i ) {
repos.add(
Repo(
name: snapshot.data.repos[i].name,
description: snapshot.data!.repos[i].description,
htmlUrl: snapshot.data!.repos[i].htmlUrl,
stargazersCount: snapshot.data!.repos[i].stargazersCount,
),
);
}
return ListView(
children: repos
.map(
(r) => Card(
color: Colors.blue[300],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
r.name,
style: const TextStyle(fontSize: 30.0),
),
Text(r.stargazersCount.toString()),
],
),
Text(
r.description,
style: const TextStyle(fontSize: 23.0),
),
Text(r.htmlUrl),
],
),
),
),
)
.toList(),
);
} else if (snapshot.hasError) {
return const Center(
child: Text('Error!'),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
),
);
}
}
This is repo.dart
class Repo {
String name;
String htmlUrl; // hmtl_url
int stargazersCount; //stargazers_count
String description;
Repo(
{required this.name,
required this.htmlUrl,
required this.stargazersCount,
required this.description});
factory Repo.fromJson(Map<String, dynamic> json) {
return Repo(
name: json['name'],
htmlUrl: json['html_url'],
stargazersCount: json['stargazers_count'],
description: json['description'],
);
}
}
class All {
List<Repo> repos;
All({required this.repos});
factory All.fromJson(List<dynamic> json) {
List<Repo> repos = <Repo>[];
repos = json.map((r) => Repo.fromJson(r)).toList();
return All(repos: repos);
}
}
class Repo {
String name;
String htmlUrl; // hmtl_url
int stargazersCount; //stargazers_count
String description;
Repo(
{required this.name,
required this.htmlUrl,
required this.stargazersCount,
required this.description});
factory Repo.fromJson(Map<String, dynamic> json) {
return Repo(
name: json['name'],
htmlUrl: json['html_url'],
stargazersCount: json['stargazers_count'],
description: json['description'],
);
}
}
class All {
List<Repo> repos;
All({required this.repos});
factory All.fromJson(List<dynamic> json) {
List<Repo> repos = <Repo>[];
repos = json.map((r) => Repo.fromJson(r)).toList();
return All(repos: repos);
}
}
CodePudding user response:
One way to make sure they are never null is to provide fallback values in the factory of Repo, for example an empty string, like this:
factory Repo.fromJson(Map<String, dynamic> json) {
return Repo(
name: json['name'] ?? "",
htmlUrl: json['html_url'] ?? "",
stargazersCount: json['stargazers_count'] ?? 0,
description: json['description'] ?? "",
);
}
CodePudding user response:
This happens when you pass a null value to a text widget.
Try adding empty string if the value is null.
Text(r.name ?? ""),