Search code examples
flutterdartwebview-flutter

Why Can't I Use WebView In Flutter Dart?


I installed webview_flutter. I ran pub get too. also, I added Import 'package:webview_flutter/webview_flutter.dart'. But actually I can't use WebView widget In flutter. This is my code. WebView occured not found error.

import 'dart:io'; // Add this import.
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewExample extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  @override
  void initState() {}
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      title: const Text('WebView Example'),
    ),
    body: Builder(builder: (BuildContext context){
      return WebView(
        initialUrl: 'https://flutter.dev',
      );
    }),
    );
  }
}

Solution

  • You need a WebViewWidget and a WebViewController

     @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: const Text('Flutter Simple Example')),
        body: WebViewWidget(controller: controller),
      );
    }
    

    See enter link description here