I’m having trouble with my flutter code

Asked

Viewed 30 times

0

I am creating an application in flutter to train but this presenting the following error:

Launching lib main.Dart on Motorola one Vision in debug mode... Running Gradle task 'assembleDebug'... Error: Cannot run with sound null Safety, because the following dependencies don’t support null Safety:

  • package:path_provider
  • package:path_provider_linux
  • package:path_provider_windows
  • package:path_provider_platform_interface
  • package:xdg_directories
  • package:plugin_platform_interface

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

FAILURE: Build failed with an Exception.

  • Where: Script 'C: src flutter Packages flutter_tools Gradle flutter.Gradle' line: 1035

  • 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 34s Exception: Gradle task assembleDebug failed with Exit code 1

the code is this:

import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import "package:path_provider/path_provider.dart";
import 'dart:async';

void main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatefulWidget {

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

class _HomeState extends State<Home> {

  List _toDoList= ["daniel","Marcos"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Lista de Tarefas"),
        backgroundColor: Colors.blueAccent,
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(17.0, 1.0, 7.0, 1.0),
            child: Row(
              children: <Widget>[
                Expanded
                  (child: TextField(
                  decoration: InputDecoration(
                    labelText: "Nova Tarefe",
                    labelStyle: TextStyle(color: Colors.blueAccent),
                  ),
                ),
                ),
                RaisedButton(
                  color: Colors.blueAccent,
                  child: Text("ADD"),
                  textColor: Colors.white,
                  onPressed: (){},
                ),
              ],
            ),
          ),
          Expanded(
              child: ListView.builder(
                padding: EdgeInsets.only(top: 10.0),
                itemCount: _toDoList.length,
                itemBuilder: (context, index){
                  return ListTile(
                    title: Text(_toDoList[index]),
                  );
                },
              ),
          ),
        ],
      ),
    );
  }
 Future<File> _getFile() async{
    final directory = await getApplicationDocumentsDirectory();
    return File("${directory.path}/data.json");
  }
  Future<File> _saveData() async{
    String data = json.encode(_toDoList);
    final file = await _getFile();
    return file.writeAsString(data);
  }
  Future<String> _readData() async{
    try{
      final file = await _getFile();

      return file.readAsString();
    }catch (e){
      return null;
    }

  }
}

1 answer

0

Raisedbutton is deprecated, Elevatedbutton is now used.

in part: Future _readData() async{ Try{ final file = await _getFile();

  return file.readAsString();
}catch (e){
  return null;
}

By the Flutter updates, the null Safety library has now been implemented, so to send Return null, is it necessary to put String? , as below:

Future<String?> _readData() async{
    try{
      final file = await _getFile();

      return file.readAsString();
    }catch (e){
      return null;
    }

And if you’re testing the app on a physical phone, you need to enable it in developer mode in the settings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.