0

I have Provider on top of MyApp, and the webview is still opening with a blank screen. No errors, no suggestions it just opening with a blank screen and not loading. If i put a web address in the url is working fine but i want to have this dynamic.

runApp(
    Provider<Events>.value(
      value: Events(),
      child: MyApp(),
    ),
  );
class Events {
  final String imagePath, site;

Events({
  this.imagePath, this.site
});

final events = [
  castel,
  lake,
];
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../model/events.dart';
import './flutter_web.dart';

class Site extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final events = Provider.of<Events>(context);

    return Container(
      padding: const EdgeInsets.all(4.0),
      child: Container(
        child: IconButton(
          onPressed: () => Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => FlutterWeb(events.site),
            ),
          ),
          icon: Icon(
            FontAwesomeIcons.internetExplorer,
            size: 30.0,
            color: Colors.lightBlue,
          ),
        ),
      ),
    );
  }
}
return WebView(
          initialUrl: events.site,
        )
1
  • What is definition of MyApp, FlutterWeb Commented Mar 6, 2020 at 5:25

1 Answer 1

1

You can copy paste run full code below
In value attribute, you need to pass variable not class
In your code snippet events is array it might be a typo

code snippet

void main() {
  final events = Events(imagePath: "castel", site: "https://flutter.dev/");

  runApp(
    Provider<Events>.value(
      value: events,
      child: MyApp(),
    ),
  );
}

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:webview_flutter/webview_flutter.dart';

class Events {
  final String imagePath, site;

  Events({this.imagePath, this.site});
}

void main() {
  final events = Events(imagePath: "castel", site: "https://flutter.dev/");

  runApp(
    Provider<Events>.value(
      value: events,
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Site());
  }
}

class Site extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var events = Provider.of<Events>(context);

    return Scaffold(
      body: Center(
        child: Container(
          padding: const EdgeInsets.all(4.0),
          child: Container(
            child: IconButton(
              onPressed: () => Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => FlutterWeb(events.site),
                ),
              ),
              icon: Icon(
                FontAwesomeIcons.internetExplorer,
                size: 30.0,
                color: Colors.lightBlue,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class FlutterWeb extends StatelessWidget {
  String site;
  FlutterWeb(this.site);

  @override
  Widget build(BuildContext context) {
    return WebView(
      initialUrl: site,
    );
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

i don't want to put final events under void main(), i have already decleared in my model/events.dart, there is a list of 30 site paths
what about if I want to add to Navigator => FlutterWeb(events.site) the imagePath?
question 1, you can directly final events = Events()
questions 2, if you events has 30 elements. you can do Navigator => FlutterWeb(events[index]) to pass Event item directly. and use it with WebView( initialUrl: event.site, )
i've tried with MapBox(events[index]) but i have error: "The operator '[]' isn't defined for the class 'Events'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.