Flutter:缺少“State.build”的具体实现

发布于 2025-01-17 22:48:50 字数 2203 浏览 0 评论 0原文

我在第 16 行收到错误: class _HomePageState extends State {

我在下面发布的小部件中已经有一个构建函数,当我尝试在 _HomePageState 中实现构建时,下面的代码无法运行。我不确定如何修复此错误,希望得到任何帮助!

import 'dart:convert';

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

void main() => runApp(MaterialApp(
      home: HomePage(),
      debugShowCheckedModeBanner: false,
    ));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

 class _HomePageState extends State<HomePage> {
   Future getUserData() async {
    var response = await http.get(Uri.parse(
    'https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m'));

var jsonData = jsonDecode(response.body);

    List users = (response.body as List?)
            ?.map((u) => User(
                u['hourly'],
                u['time'],
                u['temperature_2m'],
                u['longitude'],
                u['generationtime_ms'],
                u['hourly_units'],
                u['latitude']))
            .toList() ??
        [];
  }
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Rangers Tool'),
    ),
    body: Center(
        child: Card(
            child: FutureBuilder(
      future: getUserData(),
      builder: (context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
          return const Center(child:        

    CircularProgressIndicator());
        } else {
          return ListView.builder(
              itemCount: snapshot.data?.length,
              itemBuilder: (context, i) {
                return ListTile(
                  title: Text(snapshot.data[i].longitude),
                  subtitle: Text(snapshot.data[i].latitude),
                );
              });
        }
      },
    ))),
  );
}

getUserData() {}

class User {
  final String? hourly,
      time,
      temperature_2m,
      longitude,
      generationtime_m,
      hourly_units,
      latitude;

  User(this.longitude, this.latitude, this.hourly, this.time,
      this.temperature_2m, this.generationtime_m, this.hourly_units);
}

I am receiving an error on line 16 : class _HomePageState extends State {

I have a build function already in a widget that I have posted below, and when I try to implement a build in the _HomePageState the code below is unable to run. Im unsure on how to fix this error and would appreciate any help!

import 'dart:convert';

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

void main() => runApp(MaterialApp(
      home: HomePage(),
      debugShowCheckedModeBanner: false,
    ));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

 class _HomePageState extends State<HomePage> {
   Future getUserData() async {
    var response = await http.get(Uri.parse(
    'https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m'));

var jsonData = jsonDecode(response.body);

    List users = (response.body as List?)
            ?.map((u) => User(
                u['hourly'],
                u['time'],
                u['temperature_2m'],
                u['longitude'],
                u['generationtime_ms'],
                u['hourly_units'],
                u['latitude']))
            .toList() ??
        [];
  }
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Rangers Tool'),
    ),
    body: Center(
        child: Card(
            child: FutureBuilder(
      future: getUserData(),
      builder: (context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
          return const Center(child:        

    CircularProgressIndicator());
        } else {
          return ListView.builder(
              itemCount: snapshot.data?.length,
              itemBuilder: (context, i) {
                return ListTile(
                  title: Text(snapshot.data[i].longitude),
                  subtitle: Text(snapshot.data[i].latitude),
                );
              });
        }
      },
    ))),
  );
}

getUserData() {}

class User {
  final String? hourly,
      time,
      temperature_2m,
      longitude,
      generationtime_m,
      hourly_units,
      latitude;

  User(this.longitude, this.latitude, this.hourly, this.time,
      this.temperature_2m, this.generationtime_m, this.hourly_units);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-01-24 22:48:50

您的build方法不在州类的范围之内。

Your build method is outside the scope of your state class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文