簡體   English   中英

錯誤:無法以健全的 null 安全運行,因為以下依賴項不支持 null 安全:

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

我正在制作教室預訂系統。

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])),
          );
        },
      ),
    );
  }
}

但是出現 null 安全錯誤。

也許數據沒有保存。

我應該在哪里修復代碼?

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 package。

  1. 打開您的pubsec.yaml
  2. 將您的get: xxxxx行更改為get: ^4.1.4 (xxxxx 可能小於4.1.4
  3. 重新獲取您的 pub 依賴項或運行flutter pub upgrade
  4. 然后重建您的應用程序。

這應該解決它。

Go 到您的 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(),
          },
        );
      }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM