Home > Enterprise >  What should I do?
What should I do?

Time:02-02

My code:

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


void main() {
  runApp(
      MaterialApp(
          theme: theme,
          home : MyApp()
      )
  );
}



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

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

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

  getData() async {
    var result = await http.get(
        Uri.parse('https://codingapple1.github.io/app/data.json'));
    var result2 = jsonDecode(result.body);
    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), Text('hi')][tab],
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: true,
          showUnselectedLabels: true,
          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: 10, itemBuilder: (c, i) {
        return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network('https://codingapple1.github.io/kona.jpg'),
              Text('좋아요 100'),
              Text('글쓴이'),
              Text(data[i]['content']),
            ]
        );
      });
    } else {
      return Text('Loading');
    }
  }
}

My error:

Launching lib/main.dart on Chrome in debug mode... Waiting for connection from debug service on Chrome... ../Downloads/flutter/packages/flutter/lib/src/widgets/framework.dart:5079:27: Error: The method 'build' isn't defined for the class 'State'.

  • 'State' is from 'package:flutter/src/widgets/framework.dart' ('../Downloads/flutter/packages/flutter/lib/src/widgets/framework.dart').
  • 'StatefulWidget' is from 'package:flutter/src/widgets/framework.dart' ('../Downloads/flutter/packages/flutter/lib/src/widgets/framework.dart'). Try correcting the name to the name of an existing method, or defining a method named 'build'. Widget build() => state.build(this); ^^^^^ Failed to compile application.

CodePudding user response:

Its very simple, your build method for MyApp class is inside your getData function, which should be outside of the function.. And thats what the error message says.

CodePudding user response:

just add a closing curly bracket ( } ) after your function getData and remove the last one to put your function in the right scop

void main() {
  runApp(
      MaterialApp(
          theme: theme,
          home : MyApp()
      )
  );
}



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

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

class _MyAppState extends State<MyApp> {
  var tab = 0;
  var data = [];
  
  getData() async {
    var result = await http.get(
        Uri.parse('https://codingapple1.github.io/app/data.json'));
    var result2 = jsonDecode(result.body);
    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), Text('hi')][tab],
        bottomNavigationBar: BottomNavigationBar(
          showSelectedLabels: true,
          showUnselectedLabels: true,
          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: 10, itemBuilder: (c, i) {
        return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network('https://codingapple1.github.io/kona.jpg'),
              Text('좋아요 100'),
              Text('글쓴이'),
              Text(data[i]['content']),
            ]
        );
      });
    } else {
      return Text('Loading');
    }
  }
}

and try to write a specific subject for your question

  • Related