I'm using the flutter_inappwebview package (version 6.1.5) in my Flutter project to create a cross-platform WebView. However, I'm getting the following error when initializing the WebView:
A platform implementation for flutter_inappwebview has not been set. Please ensure that an implementation of InAppWebViewPlatform has been set to InAppWebViewPlatform.instance before use. For unit testing, InAppWebViewPlatform.instance can be set with your own test implementation. 'package:flutter_inappwebview_platform_interface/src/in_app_webview/platform_inappwebview_widget.dart': Failed assertion: line 215 pos 7: 'InAppWebViewPlatform.instance != null'
Here’s the initialization code I’m using:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; // Correct import
import 'screen/splash_screen.dart'; // Ensure you have the correct path
// Initialize WebView environment for Windows
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if (defaultTargetPlatform == TargetPlatform.windows) {
try {
final availableVersion = await WebViewEnvironment.getAvailableVersion();
assert(availableVersion != null, 'WebView2 Runtime not found.');
// Initialize WebView environment with a custom user data folder
await WebViewEnvironment.create(
settings: WebViewEnvironmentSettings(userDataFolder: 'inappwebviewdata')
);
} catch (e) {
print("Error initializing WebView2: $e");
// Handle WebView2 initialization failure (maybe show a user-friendly message)
}
}
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: SplashScreen(), // Your splash screen
);
}
}
And this is the code for my HomeScreen:
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
late InAppWebViewController webViewController;
double progress = 0.0; // Track page load progress
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('WebView Example'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
webViewController.reload();
},
),
],
),
body: Column(
children: <Widget>[
// Progress Indicator
progress < 1.0
? LinearProgressIndicator(value: progress)
: Container(),
// WebView
Expanded(
child: InAppWebView(
initialUrlRequest: URLRequest(url: WebUri(";)),
onWebViewCreated: (controller) {
webViewController = controller;
},
onLoadStart: (controller, url) {
print("Started: $url");
setState(() {
progress = 0.1; // Start loading progress
});
},
onLoadStop: (controller, url) async {
print("Stopped: $url");
setState(() {
progress = 1.0; // Complete loading
});
},
onProgressChanged: (controller, progress) {
setState(() {
this.progress = progress / 100;
});
},
onReceivedError: (controller, request, error) {
print("Error: $error");
// Handle error here, you might want to show a custom error screen.
},
),
),
],
),
);
}
}