From b41727313434ce4e8796506ee197a55157ad0324 Mon Sep 17 00:00:00 2001 From: Jon Mountjoy Date: Thu, 4 Mar 2021 22:32:56 +0000 Subject: [PATCH 1/2] null safety --- example/pubspec.yaml | 2 +- lib/flutter_user_agent.dart | 18 +++++++++--------- pubspec.yaml | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 1dddec0..97ab57a 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: 'none' version: 1.0.0+1 environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: diff --git a/lib/flutter_user_agent.dart b/lib/flutter_user_agent.dart index 11312ef..fbfb74c 100644 --- a/lib/flutter_user_agent.dart +++ b/lib/flutter_user_agent.dart @@ -5,7 +5,7 @@ import 'package:flutter/services.dart'; class FlutterUserAgent { static const MethodChannel _channel = MethodChannel('flutter_user_agent'); - static Map _properties; + static Map? _properties; /// Initialize the module. /// @@ -16,7 +16,7 @@ class FlutterUserAgent { static Future init({force: false}) async { if (_properties == null || force) { _properties = - Map.unmodifiable(await _channel.invokeMethod('getProperties')); + Map.unmodifiable(await (_channel.invokeMethod('getProperties') as FutureOr>)); } } @@ -27,28 +27,28 @@ class FlutterUserAgent { } /// Returns the device's user agent. - static String get userAgent { - return _properties['userAgent']; + static String? get userAgent { + return _properties!['userAgent']; } /// Returns the device's webview user agent. - static String get webViewUserAgent { - return _properties['webViewUserAgent']; + static String? get webViewUserAgent { + return _properties!['webViewUserAgent']; } /// Fetch a [property] that can be used to build your own user agent string. static dynamic getProperty(String property) { - return _properties[property]; + return _properties![property]; } /// Fetch a [property] asynchronously that can be used to build your own user agent string. static dynamic getPropertyAsync(String property) async { await init(); - return _properties[property]; + return _properties![property]; } /// Return a map of properties that can be used to generate the user agent string. - static Map get properties { + static Map? get properties { return _properties; } } diff --git a/pubspec.yaml b/pubspec.yaml index 4617469..f4dc206 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,11 +2,11 @@ name: flutter_user_agent description: Retrieve device and webview user agent strings for Android and iOS devices. It also provides you with simple building blocks for generating your own user agent string. -version: 1.2.2 +version: 2.0.0 homepage: https://github.com/j0j00/flutter_user_agent environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: From c8b204454438c20c516bdd73581abe74e44c9b02 Mon Sep 17 00:00:00 2001 From: Jon Mountjoy Date: Thu, 4 Mar 2021 22:55:27 +0000 Subject: [PATCH 2/2] null safety --- example/ios/Podfile | 63 ++++++++++--------------------------- example/lib/main.dart | 6 ++-- lib/flutter_user_agent.dart | 2 +- 3 files changed, 20 insertions(+), 51 deletions(-) diff --git a/example/ios/Podfile b/example/ios/Podfile index d077b08..a4af0a9 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -1,5 +1,5 @@ # Uncomment this line to define a global platform for your project -# platform :ios, '9.0' +platform :ios, '9.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' @@ -10,60 +10,29 @@ project 'Runner', { 'Release' => :release, } -def parse_KV_file(file, separator='=') - file_abs_path = File.expand_path(file) - if !File.exists? file_abs_path - return []; +def flutter_root + generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) + unless File.exist?(generated_xcode_build_settings_path) + raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" end - pods_ary = [] - skip_line_start_symbols = ["#", "/"] - File.foreach(file_abs_path) { |line| - next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } - plugin = line.split(pattern=separator) - if plugin.length == 2 - podname = plugin[0].strip() - path = plugin[1].strip() - podpath = File.expand_path("#{path}", file_abs_path) - pods_ary.push({:name => podname, :path => podpath}); - else - puts "Invalid plugin specification: #{line}" - end - } - return pods_ary + + File.foreach(generated_xcode_build_settings_path) do |line| + matches = line.match(/FLUTTER_ROOT\=(.*)/) + return matches[1].strip if matches + end + raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" end -target 'Runner' do - # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock - # referring to absolute paths on developers' machines. - system('rm -rf .symlinks') - system('mkdir -p .symlinks/plugins') +require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) - # Flutter Pods - generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig') - if generated_xcode_build_settings.empty? - puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first." - end - generated_xcode_build_settings.map { |p| - if p[:name] == 'FLUTTER_FRAMEWORK_DIR' - symlink = File.join('.symlinks', 'flutter') - File.symlink(File.dirname(p[:path]), symlink) - pod 'Flutter', :path => File.join(symlink, File.basename(p[:path])) - end - } +flutter_ios_podfile_setup - # Plugin Pods - plugin_pods = parse_KV_file('../.flutter-plugins') - plugin_pods.map { |p| - symlink = File.join('.symlinks', 'plugins', p[:name]) - File.symlink(p[:path], symlink) - pod p[:name], :path => File.join(symlink, 'ios') - } +target 'Runner' do + flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) end post_install do |installer| installer.pods_project.targets.each do |target| - target.build_configurations.each do |config| - config.build_settings['ENABLE_BITCODE'] = 'NO' - end + flutter_additional_ios_build_settings(target) end end diff --git a/example/lib/main.dart b/example/lib/main.dart index ba9a7de..9f5a708 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -12,8 +12,8 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _userAgent = ''; - String _webUserAgent = ''; + String? _userAgent = ''; + String? _webUserAgent = ''; @override void initState() { @@ -23,7 +23,7 @@ class _MyAppState extends State { // Platform messages are asynchronous, so we initialize in an async method. Future initUserAgentState() async { - String userAgent, webViewUserAgent; + String? userAgent, webViewUserAgent; // Platform messages may fail, so we use a try/catch PlatformException. try { userAgent = await FlutterUserAgent.getPropertyAsync('userAgent'); diff --git a/lib/flutter_user_agent.dart b/lib/flutter_user_agent.dart index fbfb74c..1ff8622 100644 --- a/lib/flutter_user_agent.dart +++ b/lib/flutter_user_agent.dart @@ -16,7 +16,7 @@ class FlutterUserAgent { static Future init({force: false}) async { if (_properties == null || force) { _properties = - Map.unmodifiable(await (_channel.invokeMethod('getProperties') as FutureOr>)); + Map.unmodifiable(await (_channel.invokeMethod('getProperties'))); } }