为什么我得到的是 null 而不是 name 的值?

Why I'm getting a null instead of the value of name?

我试图在 he/she 登录后在另一个页面上显示用户名。我在另一个页面上显示代码或消息没有问题。但是当我试图显示姓名或电子邮件时,我得到了一个空值。这是 print(data['name']); -> I/flutter ( 4896): null 的回复,这是 print(data['code']); -> I/flutter ( 4896): 0 的回复。我尝试添加这个 ?? '' 就像这个 pageRoute(data['name'] ?? ''); 以避免下面的错误:

E/flutter ( 4896): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
E/flutter ( 4896): #0      _MyHomePageState.login (package:login/main.dart:99:23)
E/flutter ( 4896): <asynchronous suspension>

当用户登录时,我仍然得到一个空值。这是 URL 如果用户输入正确的电子邮件和密码的响应。

I/flutter ( 4896): 200
I/flutter ( 4896): {"code":0,"message":"success","data":{"Id":121106,"Name":"John",
                    "Email":"user.user@gmail.com","Token":"4ca34eb1-0416-43f9-8d3e-19ed51d64288"}}

下面是我的代码:

Future<void> login() async {
    if(emailController.text.isNotEmpty && passController.text.isNotEmpty) {
      var headers =  {"Content-type": "application/json"};
      var myBody = {
        'email' : emailController.text,
        'password' : passController.text,
      };
      var response = await http.post(Uri.parse("url"),
          headers: headers,
          body: jsonEncode( myBody ));

      final data = jsonDecode(response.body);

      if(response.statusCode == 200 && data['code'] == 0) {
        pageRoute(data['name'] ?? '');
      } else {
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Invalid Credentials.")));
      }
    } else {
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Blank Field Not Allowed")));
    }
  }

  void pageRoute(String name) async {
    //here we store value inside shared preferences
    SharedPreferences pref = await SharedPreferences.getInstance();
    await pref.setString("login", name);

    Navigator.push(context, MaterialPageRoute(builder: (context) => const Home()));
  }
}

home.dart

String name= "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getName();
  }

  Future<void> getName() async {
    //here we fetch our credentials from shared pref
    SharedPreferences pref = await SharedPreferences.getInstance();
    setState(() {
      name = pref.getString("login")!;
    });
  }

//

@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: SafeArea(
                child: Center(
                    child: Column(
                      children: [
                        Image.asset(
                          'profile_image.jpg', height: 150, width: 150,),
                        const SizedBox(height: 15,),
                        Text("Welcome: $name"), //still null
                        const SizedBox(height: 15,),
                        const Text('Logout'),
                      ],
                    )
                 )
              ),
           ),
        )
     );

data['name']更改为data['data']['Name']