aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 245 forks source link

when ever i am trying to do flutter run then a error occurs #4570

Closed amantiwari-18 closed 6 months ago

amantiwari-18 commented 6 months ago

Description

/C:/Users/noobg/AppData/Local/Pub/Cache/hosted/pub.dev/amplify_secure_storage-0.4.2/lib/src/amplify_secure_storage.vm.dart:138:27: Error: The method 'getApplicationSupportDirectory' isn't defined for the class 'AmplifySecureStorage'.

Categories

Steps to Reproduce

No response

Screenshots

Capture

Platforms

Flutter Version

Flutter 3.19.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision ba39319843 (12 days ago) • 2024-03-07 15:22:21 -0600 Engine • revision 2e4ba9c6fb Tools • Dart 3.3.1 • DevTools 2.31.1

Amplify Flutter Version

0.6.10

Deployment Method

Amplify CLI

Schema

No response

Jordan-Nelson commented 6 months ago

@amantiwari-18 can you provide the output of 'flutter pub deps --no-dev --style=compact' and the code you are using the configure amplify?

amantiwari-18 commented 6 months ago

@amantiwari-18 can you provide the output of 'flutter pub deps --no-dev --style=compact' and the code you are using the configure amplify?


Error on line 42, column 18 of pubspec.yaml: Invalid version constraint: Expected version number after "^" in "^2.1 .2", got "2.1 .2". ╷ 42 │ path_provider: ^2.1 .2 │ ^^^^^^^

it says this when i write flutter pub deps --no-dev --style=compact


following is the code i use to congiure my amplify


import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:eadmin/auth/login.dart';
import 'package:eadmin/directional/dir.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
   await requestStoragePermission(); // Request storage permission
  await configureAmplify();

  bool isAuthenticated = await checkAuthentication();

  runApp(MyApp(isAuthenticated: isAuthenticated));
}
Future<void> configureAmplify() async {

  try {

 await Amplify.addPlugin(AmplifyDataStore(modelProvider: ModelProvider.instance)); 

    final storage = AmplifyStorageS3();
    final api = AmplifyAPI();
    final auth = AmplifyAuthCognito();
    await Amplify.addPlugins([
     api,
    storage,  
    auth,
    ]);
     await Amplify.configure(amplifyconfig);
    DynamoDB(region: 'eu-west-1');
  } 
  catch (e) {
    print("********************$e");
  }
}

Future<bool> checkAuthentication() async {
  try {
    final session = await Amplify.Auth.fetchAuthSession();
    return session.isSignedIn;
  } catch (e) {
    print("Error checking authentication: $e");
    return false;
  }
}

class MyApp extends StatelessWidget {
  final bool isAuthenticated;

  const MyApp({Key? key, required this.isAuthenticated}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: const Color.fromARGB(5, 255, 255, 255),
      ),
      home: isAuthenticated ? const dir() : const LoginScreen(),
    );
  }
}
Jordan-Nelson commented 6 months ago

It looks like you have a space in the version number '2.1 .2'. Can you remove that and then run it?

amantiwari-18 commented 6 months ago

It looks like you have a space in the version number '2.1 .2'. Can you remove that and then run it?


sorry my bad i am new to flutter this is what it says ╵ PS D:\STUDY\eadmin> flutter pub deps --no-dev --style=compact Dart SDK 3.3.1 Flutter SDK 3.19.3 eadmin 1.0.0+1

dependencies:

transitive dependencies:

amantiwari-18 commented 6 months ago

It looks like you have a space in the version number '2.1 .2'. Can you remove that and then run it?

@Jordan-Nelson any updates?

khatruong2009 commented 6 months ago

Hi @amantiwari-18, have you tried rerunning flutter clean and flutter run after making the change to the version number?

amantiwari-18 commented 6 months ago

@khatruong2009 @Jordan-Nelson

hey i tired flutter clean and flutter run but the error persists

PS D:\STUDY\eadmin> flutter clean Deleting build... 1,764ms Deleting .dart_tool... 66ms Deleting Generated.xcconfig... 3ms Deleting flutter_export_environment.sh... 7ms Deleting ephemeral... 14ms Deleting ephemeral... 42ms Deleting ephemeral... 4ms Deleting .flutter-plugins-dependencies... 19ms Deleting .flutter-plugins... 10ms PS D:\STUDY\eadmin> flutter run Resolving dependencies... (1.5s) built_value 8.8.1 (8.9.1 available) drift 2.14.1 (2.16.0 available) http 0.13.6 (1.2.1 available) intl 0.18.1 (0.19.0 available) js 0.6.7 (0.7.1 available) leak_tracker 10.0.0 (10.0.4 available) leak_tracker_flutter_testing 2.0.1 (3.0.3 available) leak_tracker_testing 2.0.1 (3.0.1 available)
material_color_utilities 0.8.0 (0.11.1 available)
meta 1.11.0 (1.12.0 available) package_info_plus 4.2.0 (5.0.1 available) table_calendar 3.0.9 (3.1.0 available) test_api 0.6.1 (0.7.0 available) uuid 3.0.7 (4.3.3 available) vm_service 13.0.0 (14.1.0 available) Got dependencies! 15 packages have newer versions incompatible with dependency constraints. Try flutter pub outdated for more information. Launching lib\main.dart on sdk gphone x86 in debug mode... /C:/Users/noobg/AppData/Local/Pub/Cache/hosted/pub.dev/amplify_secure_storage-0.4.2/lib/src/amplify_secure_storage.vm.dart:138:27: Error: The method 'getApplicationSupportDirectory' isn't defined for the class 'AmplifySecureStorage'.

FAILURE: Build failed with an exception.

BUILD FAILED in 1m 33s Running Gradle task 'assembleDebug'... 97.3s Error: Gradle task assembleDebug failed with exit code 1

amantiwari-18 commented 6 months ago

Hi @amantiwari-18, have you tried rerunning flutter clean and flutter run after making the change to the version number?

@Jordan-Nelson @khatruong2009 Any suggestions guys how can i overcome this error?

Jordan-Nelson commented 6 months ago

Can you share the contents of these two files?

The error message is saying that the function getApplicationLocalSupportDirectory() isn't defined. It should be defined in path_provider_local.dart. I would like to see the contents of your local files to see if that function is defined.

amantiwari-18 commented 6 months ago
  • /C:/Users/noobg/AppData/Local/Pub/Cache/hosted/pub.dev/amplify_secure_storage-0.4.2/lib/src/path_provider_local.dart

@Jordan-Nelson @khatruong2009

this is the path_provider

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// ignore_for_file: implementation_imports

import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:path_provider_windows/src/folders.dart';
import 'package:path_provider_windows/src/path_provider_windows_real.dart';

/// A version of [PathProviderWindows] that uses the
/// [WindowsKnownFolder.LocalAppData] directory in place of the
/// [WindowsKnownFolder.RoamingAppData] directory.
class PathProviderWindowsLocal extends PathProviderWindows {
  @override
  Future<String?> getPath(String folderID) {
    if (folderID == WindowsKnownFolder.RoamingAppData) {
      return super.getPath(WindowsKnownFolder.LocalAppData);
    }
    return super.getPath(folderID);
  }
}

// / Returns version of [getApplicationSupportDirectory] that returns the Local
/// App Data directory on Windows.
Future<Directory> getApplicationLocalSupportDirectory() async {
  if (!Platform.isWindows) throw UnsupportedError('Only supported on Windows');
  final path = await PathProviderWindowsLocal().getApplicationSupportPath();
  if (path == null) {
    throw MissingPlatformDirectoryException(
      'Unable to get application support directory',
    );
  }
  return Directory(path);
}
amantiwari-18 commented 6 months ago

Can you share the contents of these two files?

  • /C:/Users/noobg/AppData/Local/Pub/Cache/hosted/pub.dev/amplify_secure_storage-0.4.2/lib/src/amplify_secure_storage.vm.dart'
  • /C:/Users/noobg/AppData/Local/Pub/Cache/hosted/pub.dev/amplify_secure_storage-0.4.2/lib/src/path_provider_local.dart

The error message is saying that the function getApplicationLocalSupportDirectory() isn't defined. It should be defined in path_provider_local.dart. I would like to see the contents of your local files to see if that function is defined.

@Jordan-Nelson @khatruong2009 this is the amplify_secure_storage.vm

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:async';
import 'dart:io';

import 'package:amplify_secure_storage/src/amplify_secure_storage.android.dart';
import 'package:amplify_secure_storage/src/path_provider_local.dart';
import 'package:amplify_secure_storage/src/pigeons/ns_user_defaults_pigeon.g.dart';
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
// ignore: implementation_imports
import 'package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart';
import 'package:async/async.dart';
import 'package:meta/meta.dart';
import 'package:path_provider/path_provider.dart';

/// {@template amplify_secure_storage.amplify_secure_storage}
/// The default Secure Storage implementation used in Amplify packages.
/// {@endtemplate}
class AmplifySecureStorage extends AmplifySecureStorageInterface {
  /// {@template amplify_secure_storage.amplify_secure_storage.from_config}
  /// Generates a [AmplifySecureStorage] from a config.
  ///
  /// Should only be used within Amplify packages. See [factoryFrom] for
  /// external use.
  /// {@endtemplate}
  @internal
  AmplifySecureStorage({
    required super.config,
  });

  /// {@template amplify_secure_storage.amplify_secure_storage.factory_from}
  /// Returns a factory for creating [AmplifySecureStorage] instances.
  /// {@endtemplate}
  static AmplifySecureStorage Function(
    AmplifySecureStorageScope amplifyScope,
  ) factoryFrom({
    WebSecureStorageOptions? webOptions,
    WindowsSecureStorageOptions? windowsOptions,
    LinuxSecureStorageOptions? linuxOptions,
    MacOSSecureStorageOptions? macOSOptions,
    IOSSecureStorageOptions? iOSOptions,
  }) {
    return (AmplifySecureStorageScope scope) {
      return AmplifySecureStorage(
        config: AmplifySecureStorageConfig(
          scope: scope.name,
          webOptions: webOptions,
          windowsOptions: windowsOptions,
          linuxOptions: linuxOptions,
          macOSOptions: macOSOptions,
          iOSOptions: iOSOptions,
        ),
      );
    };
  }

  late final AmplifySecureStorageInterface _instance;

  /// A namespace for storing the list of registered scopes.
  ///
  /// Used as the file name on Linux and a prefix for the
  /// key in NSUserDefaults on iOS and MacOS.
  @visibleForTesting
  static const scopeStoragePrefix = 'amplify_secure_storage_scopes';

  final _initMemo = AsyncMemoizer<void>();

  Future<void> _init() async {
    await _initMemo.runOnce(
      () async {
        if (Platform.isAndroid) {
          _instance = AmplifySecureStorageAndroid(config: config);
        } else {
          var config = this.config;
          if (Platform.isWindows) {
            config = config.copyWith(
              windowsOptions: config.windowsOptions.copyWith(
                storagePath: config.windowsOptions.storagePath ??
                    (await getApplicationLocalSupportDirectory()).path,
              ),
            );
          }
          // ignore: invalid_use_of_internal_member
          _instance = AmplifySecureStorageWorker(config: config);
        }
        if (Platform.isIOS || Platform.isMacOS || Platform.isLinux) {
          final accessGroup = Platform.isLinux
              ? config.linuxOptions.accessGroup
              : Platform.isIOS
                  ? config.iOSOptions.accessGroup
                  : config.macOSOptions.accessGroup;
          // if accessGroup is set, do not clear data on initialization
          // since the data can be shared across applications.
          if (accessGroup == null) {
            await _initializeScope();
          }
        }
      },
    );
  }

  @override
  Future<void> delete({required String key}) async {
    await _init();
    return _instance.delete(key: key);
  }

  @override
  Future<String?> read({required String key}) async {
    await _init();
    return _instance.read(key: key);
  }

  @override
  Future<void> write({required String key, required String value}) async {
    await _init();
    return _instance.write(key: key, value: value);
  }

  @override
  Future<void> removeAll() async {
    await _init();
    // ignore: invalid_use_of_internal_member
    return _instance.removeAll();
  }

  /// Clears all keys for the given scope if this scope
  /// has not been initialized previously.
  ///
  /// Checks for an initialization flag in file storage.
  /// If the flag is not present storage will be cleared
  /// and then the flag will be set.
  ///
  /// Intended to clear storage after an app uninstall & re-install.
  Future<void> _initializeScope() async {
    if (Platform.isLinux) {
      final path = (await getApplicationSupportDirectory()).path;
      final fileStore = FileKeyValueStore(
        path: path,
        fileName: '$scopeStoragePrefix.json',
      );
      final isInitialized = await fileStore.containsKey(key: config.scope!);
      if (!isInitialized) {
        // removeAll is marked as internal to prevent use from outside
        // of secure_storage. Use in amplify_secure_storage is acceptable.
        // ignore: invalid_use_of_internal_member
        await _instance.removeAll();
        await fileStore.writeKey(key: config.scope!, value: true);
      }
    }

    if (Platform.isIOS || Platform.isMacOS) {
      final userDefaults = NSUserDefaultsPigeon();
      final key = '$scopeStoragePrefix.${config.scope}.isKeychainConfigured';
      final isInitialized = await userDefaults.boolFor(key);
      if (!isInitialized) {
        // ignore: invalid_use_of_internal_member
        await _instance.removeAll();
        await userDefaults.setBool(key, true);
      }
    }
  }
}
Jordan-Nelson commented 6 months ago

Thanks. The function that cannot be found should be coming from the path_provider plugin. Can you share the contents of the following file?

amantiwari-18 commented 6 months ago
  • path_provider-2.1.2/lib/path_provider.dart

@Jordan-Nelson Thanks a lot man the path_finder file was empty even after downloading the package multiple times

i copied the path finder code from github and pasted now it works