Home > Software design >  Flutter File_picker : 'platform' isn't defined
Flutter File_picker : 'platform' isn't defined

Time:11-27

I'm trying to implement some file management in my desktop ( windows ) flutter project, so I added the file_picker package from pub.dev, see the link here : https://pub.dev/packages/file_picker.

Following the official documentation, I first tried to add the logic in a separate function to try it before plug the system to my app, see my code below:

testFilePicker() async {
    String? savePathFile = await FilePicker.platform.saveFile(
        dialogTitle: 'Select a location to save your file',
        fileName: "test_file");
  }

This piece of code is suppose to open a window where I could choose a path to save a file.

Here comes my problem : my ide (vscode) highlights "platform" as a critical error, see the error message below :

The name 'platform' is being referenced through the prefix 'FilePicker', but it isn't defined in any of the libraries imported using that prefix.

Of course, I did import file_picker package and prefix it as "FilePicker", this way :

import 'package:file_picker/file_picker.dart' as FilePicker;

So the problem musn't be there, I also tried to uninstall pacakge and re-import it but the problem persists.

I am currently stuck on this and I can't find any solution, any help would be appreciate.

Thanks you in advance.

CodePudding user response:

Rather than importing with as FilePicker;, import the library without a prefix.

import 'package:file_picker/file_picker.dart';

Alternatively, you could use a prefix like so:

import 'package:file_picker/file_picker.dart' as filepicker;

...

String? savePathFile = await filepicker.FilePicker.platform.saveFile(
  dialogTitle: 'Select a location to save your file',
  fileName: "test_file");

Full code example:

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(onPressed: () async {
              String? savePathFile = await FilePicker.platform.saveFile(
                  dialogTitle: 'Select a location to save your file',
                  fileName: "test_file");
            }, child: const Text('Save file')),
          ],
        ),
      ),
    );
  }
}
  • Related