NoSuchMethodError: Class 'bool' has no instance method 'call'. Receiver: true Tried calling: call() The relevant error-causing widget was Home

Viewed 319

The following error occurred while cloning Instagram. If the if statement is removed, the output is normal, but if an if statement is added, an error occurs. I can't get a hold of it at all.

The error message as below. Class 'bool' has no instance method 'call'. Receiver: true Tried calling: call()

My code is below.

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

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: style.theme,
      home: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var tab = 1;
  var data = [];

  getData() async {
    var result = await http.get(
      Uri.parse('https://codingapple1.github.io/app/data.json'),
    );
    var result2 = jsonDecode(result.body); //jsonDecode(): json을 list나 map자료로 변환
    setState(() {
      data = result2;
    });
  }

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
     appBar: AppBar(
        title: Text(
          'Instagram',
        ),
        actions: [
          IconButton(
            icon: Icon(Icons.add_box_outlined),
            onPressed: () {},
            iconSize: 30,
          ),
        ],
      ),
      body: Home(data: data),
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: false,
        showUnselectedLabels: false,
        onTap: (i) {
          setState(() {
            tab = i;
          });
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home_outlined), label: '홈'),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_bag_outlined), label: '샵'),
        ],
      ),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key, this.data}) : super(key: key);
  final data;

  @override
  Widget build(BuildContext context) {
    if (data.isNotEmpty()) {
      return ListView.builder(
        itemCount: 3,
        itemBuilder: (context, index) {
          return SizedBox(
            height: 380,
            child: Column(
              children: [
                Image.network(data[index]['image'], width: 300),
                SizedBox(
                  width: 300,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '좋아요 ${data[index]['likes']}',
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      Text(data[index]['user']),
                      Text(data[index]['content']),
                    ],
                  ),
                )
              ],
            ),
          );
        },
      );
    } else {
      return Text('로딩중임');
    }
  }
}

enter image description here

2 Answers

Class 'bool' has no instance method 'call'. Receiver: true

isNotEmpty is a property, not a method, replace isNotEmpty() to isNotEmpty and will work fine

First delete this lines because we will call getData into futureBuilder

 @override
  void initState() {
    super.initState();
    getData();
  }

then make your listview.builder into futureBuilder with getData() function data with snapshot and replace isNotEmpty() with hasData like the code below

FutureBuilder(
future: getData(),  
builder: ((context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.data.hasData) {
      return ListView.builder(
        itemCount: 3,
        itemBuilder: (context, index) {
          return SizedBox(
            height: 380,
            child: Column(
              children: [
                Image.network(snapshot.data[index]['image'], width: 300),
                SizedBox(
                  width: 300,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '좋아요 ${snapshot.data[index]['likes']}',
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      Text(snapshot.data[index]['user']),
                      Text(snapshot.data[index]['content']),
                    ],
                  ),
                )
              ],
            ),
          );
        },
      );
    } else {
      return Text('로딩중임');
    }
  }
Related