rrousselGit / provider

InheritedWidgets, but simple
https://pub.dev/packages/provider
MIT License
5.11k stars 512 forks source link

Provider.of<AppData>(context); causing Unsupported operation: Platform._operatingSystem #636

Closed letto4135 closed 3 years ago

letto4135 commented 3 years ago

I'm having an issue with provider package on web it seems. I've also tried context.watch(); in place of Provider.of(context); but have the same error. The first print statement runs, the second does not.


import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';
import 'AppData.dart';
import 'VærBurger.dart';
import 'bitsAndBobs/AdBar.dart';
import 'bitsAndBobs/Astro.dart';
import 'bitsAndBobs/ChartsHourly.dart';
import 'bitsAndBobs/ChartsDaily.dart';
import 'bitsAndBobs/MyLocation.dart';
import 'bitsAndBobs/Summary.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Future<InitializationStatus> _initGoogleMobileAds() {
    return MobileAds.instance.initialize();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        home: ChangeNotifierProvider(
          create: (context) => AppData(context),
          child: MyHomePage(title: 'Flutter Demo Home Page'),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    print("test");
    final appData = Provider.of<AppData>(context);
    print("test2");
    final theme = appData.backgroundColor != null
        ? appData.backgroundColor[400]
        : appData.backgroundColor;
    final background =
        appData.theme != null ? appData.theme[400] : appData.defaultThemeColor;

    List<Widget> children = [
      AdBar(),
      MyLocation(),
      Summary(),
      Astro(),
    ];

    Map<String, List<String>> weatherData = {
      'temp': ['day', 'night'],
      'feels_like': ['day', 'night'],
      'humidity': [],
    };

    weatherData.forEach((key, values) {
      children.add(Container(height: 20));
      if (appData.dataWanted == 0 || appData.dataWanted == 1) {
        children.add(ChartsHourly(key));
      } else {
        children.add(ChartsDaily(key, values));
      }
    });

    return Container(
      child: Scaffold(
        backgroundColor: background,
        drawer: VaerBurger(),
        appBar: AppBar(
          backgroundColor: theme,
          title: Text("Weather App"),
        ),
        body: SingleChildScrollView(
          child: Column(mainAxisSize: MainAxisSize.min, children: children),
        ),
      ),
    );
  }
}
rrousselGit commented 3 years ago

Provider is not using Platform anywhere. This is most definitely a bug on your side

Chances are calling Provider.of initialized the provider, and your object created (here AppData) used Platform

letto4135 commented 3 years ago

5 months later I've already passed the class this was for and have no idea what I did to fix the issue. Pretty sure I switched to a different library tho.