我收到了这个错误

无法使用静态访问访问实例成员

我找不到位置,因为我在另一个类中有与错误相关的数据.我刚开始学dart ,还在学习,所以这一切对我来说都没有多大意义.

我收到此错误的代码为.getLocationWeather():

 void getLocationData() async {
    var weatherData = await WeatherModel.getLocationWeather();

    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return LocationScreen(
        locationWeather: weatherData,
      );

下面是getLlocationWeather()所在的类的代码.

import 'location.dart';
import 'networking.dart';

const apiKey = 'e72ca729af228beabd5d20e3b7749713';
const openWeatherMapURL = 'https://api.openweathermap.org/data/2.5/weather';

class WeatherModel {
  Future<dynamic> getCityWeather(String cityName) async {
    NetworkHelper networkHelper = NetworkHelper(
        '$openWeatherMapURL?q=$cityName&appid=$apiKey&units=metric');

    var weatherData = await networkHelper.getData();
    return weatherData;
  }

  Future<dynamic> getLocationWeather() async {
    Location location = Location();
    await location.getCurrentLocation();

    NetworkHelper networkHelper = NetworkHelper(
        '$openWeatherMapURL?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey&units=metric');

    var weatherData = await networkHelper.getData();
    return weatherData;
  }

推荐答案

只需在周围添加括号即可使其成为非静态的.具体如下:

var weatherData = await WeatherModel().getLocationWeather();

Flutter相关问答推荐

使用Flutter 提供程序包加载状态应用程序时,在自定义按钮中显示CircularProgressIndicator

如何在凌晨12点显示00:00(24小时格式)而不是在Flutter 中显示24:00

Dexing时出错.将Flutter 升级到3.19.0后

如何在2024年使用Ffltter将用户数据保存在云FireStore中?

material 3芯片,不能go 除输入芯片小部件上的轮廓

使用GO_ROUTER在模式内导航

点击表情符号按钮后,Flutter 文本域文本编辑控制器将文本恢复为原始值

将Riverpods NotifierProvider与State类一起使用

将记录/元组用作SplayTreeMap中的键

如何处理平台游戏的对象冲突?

带有附加图像的ListTile

Riverpod 2.0-如何使用FutureOr和AutoDisposeSyncNotificationer处理api状态代码?

Flutter:注册 UI 出现 UI 问题(将注册框提升到顶部并向电话号码 pin 码添加一个框)

Flutter记录返回类型问题

如何使Flutter 边框宽度在外面而不是在里面?

在Flutter中为容器实现线性渐变背景动画

如何停止折线图的抖动

flutter 创建一个带有 2 个图标的按钮

如何将所有行项目平等地放在一行中?Flutter

参数类型Future>不能分配给参数类型Future>?