flutter 我该怎么办?[关闭]

djp7away  于 2023-02-05  发布在  Flutter
关注(0)|答案(2)|浏览(99)

9小时前关门了。
Improve this question
我的代码:

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');
    }
  }
}

我的错误:
正在调试模式下启动Chrome浏览器上的lib/main. dart ...正在等待Chrome浏览器上调试服务的连接... ../下载/flutter/packages/flutter/lib/src/widgets/framework. dart:5079:27:错误:没有为类"State"定义方法"build"。

  • "状态"来自"软件包:flutter/src/小部件/框架. dart"('../下载/flutter/软件包/flutter/lib/src/小部件/框架. dart ')。
  • "StatefulWidget"来自"package:flutter/src/widgets/framework. dart"('../Downloads/flutter/packages/flutter/lib/src/widgets/framework. dart')。请尝试将名称更正为现有方法的名称,或定义名为"build"的方法。Widget build()=〉www.example.com(this);^^^^^未能编译应用程序。state.build(this); ^^^^^ Failed to compile application.
iezvtpos

iezvtpos1#

这非常简单,MyApp类的构建方法在getData函数内部,而getData函数应该在函数外部。

oyxsuwqo

oyxsuwqo2#

只需在函数getData后面添加一个右花括号(}),然后删除最后一个,以便将函数放在正确的范围中

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');
    }
  }
}

试着为你的问题写一个具体的主题

相关问题