Home > database >  Flutter: type Null is not a subtype of type 'int'
Flutter: type Null is not a subtype of type 'int'

Time:05-31

I am new to Flutter and mobile apps development.

So, I am stuck with this error for week, trying and reading the solution over here but still unable to solve my problem.

So, this the API that I want to get the value from:

{"error":false,"errmsg":"","data":[{"site":"hadapan","data1":"25.00","data3":"0.00"}]}

And this the code I got from an example and edit to use from my api.

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<Album> fetchAlbum() async {
  final response = await http
      .get(Uri.parse('https://localhost/data.php'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return Album.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

class Album {
  final String site;
  final int data1;
  final int data3;

  const Album({
    required this.site,
    required this.data1,
    required this.data3,
  });

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      site: json['data'][0]['site'] as String, //data
      data1: json['data'][0]['data1'] as int,
      data3: json['data'][0]['data3'] as int,
    );
  }
}

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late Future<Album> futureAlbum;

  @override
  void initState() {
    super.initState();
    futureAlbum = fetchAlbum();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Fetch Data Example'),
        ),
        body: Center(
          child: FutureBuilder<Album>(
            future: futureAlbum,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data!.site); //I am trying to pass the data here
              } else if (snapshot.hasError) {
                return Text('${snapshot.error}');
              }

              // By default, show a loading spinner.
              return const CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}

Hope someone can point out where I do wrong.

Appreciate the help.

CodePudding user response:

I think some of the values came as null you probably should make the properties nullable ex: final int? data1;

CodePudding user response:

Your API is blank, no data, please check your api https://localhost/data.php

CodePudding user response:

Make it Nullable For Example

Replace this

final String site;
final int data1;
final int data3;

With This

final String? site;
final int? data1;
final int? data3;
  • Related