必须向文本小部件提供非空字符串。/失败断言:第 380 行 pos 10:'data != null'

时间:2021-01-05 19:13:57

标签: json flutter

我只是想从互联网上获取天气预报数据。我在下面写了这段代码,但每次出现错误时:

<块引用>

必须为文本小部件提供非空字符串

由于某种原因,我无法获取解码的 json 数据

import 'package:flutter/material.dart';
import 'network.dart';

class HomePage extends StatefulWidget {
  static String id;

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Map<String,dynamic> currentPositionWeatherData;
  String temperature;
  String weatherDescription;
  String name;


  void initState(){
    super.initState();
    getCurrentLocationWeather();
    updateUI();
  }

  void getCurrentLocationWeather()async{
    currentPositionWeatherData=await NetworkHelperForWeatherInfo().getCurrentPositionWeatherData();
  }
  void updateUI()async{
    setState(() {
      weatherDescription = currentPositionWeatherData["weather"][0]["description"];
      temperature = currentPositionWeatherData["main"]["temp"];
      name = currentPositionWeatherData["name"];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.black,
        child: Text(
          temperature,
        ),
      ),
    );
  }
}

import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart';
const openWeatherURL="http://api.openweathermap.org/data/2.5/weather";
const apikey="57aad03f4e48ca815bb1184e74624f46";

class NetworkHelperForWeatherInfo{

  Future getCurrentPositionWeatherData()async{
    Position position = await Geolocator.getCurrentPosition();
    Response response = await get("$openWeatherURL?lat=${position.latitude}&lon=${position.latitude}&appid=$apikey");
    if(response.statusCode ==200){
      String currentPositionWeatherData = response.body;
      return jsonDecode(currentPositionWeatherData);
    }
    else{
      return response.statusCode;
    }
  }


}

import 'package:flutter/material.dart';
import 'homepage.dart';
import 'package:geolocator/geolocator.dart';

class LocationFinder{
  bool serviceEnabled;
  LocationPermission permission;

  Future<Position> determineCurrentPosition()async{
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if(serviceEnabled != true){
      return Future.error("Location services are disabled");
    }
    permission = await Geolocator.checkPermission();
    if (permission==LocationPermission.deniedForever) {
      return Future.error("Location permissions are permantly denied,we cannot request permissions.");
    }
    if (permission == LocationPermission.denied){
      permission = await Geolocator.requestPermission();
      if(permission != LocationPermission.whileInUse &&
          permission != LocationPermission.whileInUse){
        return Future.error("Location permission are denied/ actual value:$permission");
      }
    }
    Position position =await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    return position;
  }
}

1 个答案:

答案 0 :(得分:0)

child: Text(
          temperature ?? 'waiting data...',
        )

首次构建 _HomePageState 状态小部件时,temperature 变量将为空(直到检索到数据并通过 setState 调用重新构建小部件)。断言文本值不为空的 Text 小部件不允许使用空值。

waiting data... 为空时,上述更改将提供文本 temperature