【问题标题】:Error: Cannot run with sound null safety, because the following dependencies don't support null safety:错误:无法以可靠的 null 安全性运行,因为以下依赖项不支持 null 安全性:
【发布时间】:2021-05-18 07:21:03
【问题描述】:

我正在制作教室预订系统。

class _SearchViewState extends State<SearchView> {
  List data = [];

  Future<String> getData() async {
    http.Response res = await http
        .get(Uri.parse("https://gcse.doky.space/api/schedule/buildings"));
    this.setState(() {
      data = jsonDecode(res.body)["result"];
    });

    return "success";
  }

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('건물 선택')),
      body: new ListView.builder(
        itemCount: data == null ? 0 : data.length,
        itemBuilder: (BuildContext context, int index) {
          return new Card(
            child: ListTile(
                onTap: () {
                  Get.to(() => SearchView2(), arguments: data[index]);
                },
                title: Text(data[index])),
          );
        },
      ),
    );
  }
}

我想将 data[index] 移动到其他视图。

这是第二个视图。

class SearchView2 extends StatefulWidget {
  @override
  _SearchViewState2 createState() => _SearchViewState2();
}

class _SearchViewState2 extends State<SearchView2> {
  String building = Get.arguments;
  List data = [];
  Future<String> getData() async {
    http.Response res = await http.get(Uri.parse(
        "https://gcse.doky.space/api/schedule/classrooms?bd=$building"));
    this.setState(() {
      data = jsonDecode(res.body)["result"];
    });

    return "success";
  }

  void _itemtapped() {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ReservationView()), //route here
    );
  }

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('강의실 선택')),
      body: new ListView.builder(
        itemCount: data == null ? 0 : data.length,
        itemBuilder: (BuildContext context, int index) {
          return new Card(
            child: ListTile(onTap: _itemtapped, title: Text(data[index])),
          );
        },
      ),
    );
  }
}

但是会出现空安全错误。

可能数据没有保存。

我应该在哪里修复代码?

Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

- package:get

For solutions, see https://dart.dev/go/unsound-null-safety
2


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
Exception: Gradle task assembleDebug failed with exit code 1

这是我运行应用程序时的错误。

我需要移动 data[index] 以使用下一个 API。

我还需要将建筑和演讲室数据发送到预订视图。

【问题讨论】:

  • 你使用的是最新版本的get包吗?

标签: flutter


【解决方案1】:

这是因为您使用的是旧版本的 getflutter 包。

  1. 打开您的pubsec.yaml
  2. 将您的get: xxxxx 行更改为get: ^4.1.4(xxxxx 可能小于4.1.4
  3. 重新获取您的 pub 依赖项或运行 flutter pub upgrade
  4. 然后重建您的应用程序。

这应该可以解决它。

【讨论】:

  • null 安全错误已修复,但“您正在尝试在没有 GetMaterialApp 或 Get.key 的情况下使用无上下文导航。如果您正在测试您的应用程序,您可以使用:[Get.testMode = true],或者,如果您在物理设备或模拟器上运行您的应用程序,则必须将您的 [MaterialApp] 换成 [GetMaterialApp]。”此错误已离开。我应该在哪里改变?
  • 您的代码开头可能有一个MaterialApp。尝试将其更改为GetMaterialApp
  • 那可能就是你使用SearchView的地方。
  • 如果我将脚手架或卡片更改为 GetMaterialApp,则会出现另一个错误
  • 没有没有。脚手架是 GetMaterialApp 内部的东西。您在哪里使用 SearchView 小部件?
【解决方案2】:

转到您的 Main.dart 并以 `

开头

GetMaterialApp

这里是演示。它会起作用的。

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getxpro/view/cartPage.dart';
import 'package:getxpro/view/home.dart';

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return GetMaterialApp( //for navigation dont forget to use GetMaterialApp
          title: 'getXpro',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          initialRoute: '/',
          routes: {
            '/': (context) => HomePage(),
            '/cart': (context) => CartPage(),
          },
        );
      }
    }

【讨论】:

    【解决方案3】:

    运行

    flutter run --no-sound-null-safety
    

    如果您想在没有健全的 null 安全性的情况下运行您的应用程序, 对于 VSCode 用户,将以下内容添加到 settings.json

    "dart.flutterRunAdditionalArgs": [
        "--no-sound-null-safety"
    ],
    

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 2021-10-23
      • 2021-03-03
      • 2021-09-02
      • 2021-09-29
      • 2021-11-19
      • 2021-09-18
      • 2021-06-04
      相关资源
      最近更新 更多