如何管理api请求后不存在的变量?

问题描述 投票:0回答:1

我有这个问题,我在api上发出一个http请求,当我在响应中情况= 0时,我应该显示一条简单的消息,并且它工作得很好,但是当响应情况不存在时,我有此消息和我的代码不会执行,但是我指定在响应情况中可能不存在。这是我收到的消息

抛出另一个异常:TypeError: null: type 'Null' is not a subtype of type 'String'

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


class GamePage extends StatefulWidget {
  String time = "";

  GamePage({
    super.key, //required this.mailc, required this.mdp
    // required GlobalKey<FormState> formKey,
    required this.mail,
    required this.password,
    //required this.controllermotdepasse
  });

  final String mail;
  final String password;


  @override
  State<GamePage> createState() => _GamePageState();
}

class _GamePageState extends State<GamePage> {

  Future<void> container() async {
    var url = Uri.parse('https://konamicash.com/vfetch_app');
    var response = await http.post(url, headers: {
      "Accept": "application/json",
      "Access-Control-Allow-Origin": "*"
    }, body: {
      "adresse_mail": widget.mail,
    });

    if (response.statusCode == 200) {
      var reponse = jsonDecode(response.body);
       String? situation = reponse['situation'];
      
      if (situation == 0) {
        print('no data avalaible');
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('no game'),
          ),
        );
      } else {// here is where situation is supposed to not exist and where i get this message //Another exception was thrown: TypeError: null: type 'Null' is not a subtype of type 'String'
        
var image_team = reponse['image_team'];
        Navigator.push(
          context,
          PageRouteBuilder(
              pageBuilder: (context, animation, secondaryAnimation) => team(
                  mail: widget.mail,
                  password: widget.password,
                  team: image_team),
              // TestVfetch(mail: widget.mail, password: widget.password),
              transitionsBuilder:
                  (context, animation, secondaryAnimation, child) {
                animation =
                    CurvedAnimation(parent: animation, curve: Curves.ease);
                return FadeTransition(
                  opacity: animation,
                  child: child,
                );
              }),
        );
      }
      print(reponse);
    }
  }
}
flutter dart
1个回答
0
投票
var image_team = response.containsKey('image_team')?reponse['image_team']:'';
    Navigator.push(
      context,
      PageRouteBuilder(
          pageBuilder: (context, animation, secondaryAnimation) => team(
              mail: widget.mail,
              password: widget.password,
              team: image_team),
          // TestVfetch(mail: widget.mail, password: widget.password),
          transitionsBuilder:
              (context, animation, secondaryAnimation, child) {
            animation =
                CurvedAnimation(parent: animation, curve: Curves.ease);
            return FadeTransition(
              opacity: animation,
              child: child,
            );
          }),
    );

“image_team”键在您的 json 响应中不可用

© www.soinside.com 2019 - 2024. All rights reserved.