在使用工作管理器插件的应用程序中使用时,颤振提供程序无法正常工作

时间:2020-12-29 07:46:13

标签: android flutter flutter-provider flutter-workmanager

我有一个应用程序,我使用 workmanger 插件,我也使用 provider 插件。

工作管理器使提供程序无法正常工作,我没有任何错误,也没有在 dubg 或终端上收到任何错误,只是它没有按预期工作,我的 streamProvider 在首先使用 firebase auth 登录后没有更新小部件时间,但是当我不在我的应用程序中使用工作管理器插件时,stereamProvider 工作正常。

所以我得出这个结论是使用 workmanger 和 streamProvider 使您的应用无法按预期运行。

当我使用 Workmanger 并实现 Streamprovider 时,这是我的 main.dart 文件

import 'package:flutter/material.dart';
import 'package:mohammad/pages/tabs.dart';
import 'package:mohammad/provider/auth_provider.dart';
import 'package:mohammad/provider/word_provider.dart';

import 'package:provider/provider.dart';
import 'package:firebase_core/firebase_core.dart';

import './models/user.dart';
import 'package:workmanager/workmanager.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:async';

import 'api/notification.dart';

void callbackDispatcher() {
  Workmanager.executeTask((task, inputData) async {
    await Firebase.initializeApp();
    bool isNewWord = await notificationChecker();
    if (!isNewWord) {
      return Future.value(true);
    }
    FlutterLocalNotificationsPlugin flip =
        new FlutterLocalNotificationsPlugin();
    // app_icon needs to be a added as a drawable
    // resource to the Android head project.
    var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var IOS = new IOSInitializationSettings();
    // initialise settings for both Android and iOS device.
    var settings = new InitializationSettings(android: android, iOS: IOS);
    flip.initialize(settings);
    _showNotificationWithDefaultSound(flip);
    //simpleTask will be emitted here.
    return Future.value(true);
  });
}

void justForTest()async{
   await Firebase.initializeApp();
    bool isNewWord = await notificationChecker();
    if (!isNewWord) {
      return;
    }
    FlutterLocalNotificationsPlugin flip =
        new FlutterLocalNotificationsPlugin();
    // app_icon needs to be a added as a drawable
    // resource to the Android head project.
    var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
    var IOS = new IOSInitializationSettings();
    // initialise settings for both Android and iOS device.
    var settings = new InitializationSettings(android: android, iOS: IOS);
    flip.initialize(settings);
    _showNotificationWithDefaultSound(flip);
    //simpleTask will be emitted here.
}

Future _showNotificationWithDefaultSound(flip) async {
  // Show a notification after every 15 minute with the first
  // appearance happening a minute after invoking the method
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'your channel id', 'your channel name', 'your channel description',
      importance: Importance.max, priority: Priority.max,enableVibration: true,enableLights: true,
       largeIcon: DrawableResourceAndroidBitmap('ic_launcher_round'),
       );
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();

  // initialise channel platform for both Android and iOS device.
  var platformChannelSpecifics = new NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics);
  await flip.show(0, 'Muhammad Mahdi', 'وشەی نوێ', platformChannelSpecifics,
      payload: 'ic_launcher_round',);
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
 Workmanager.initialize(
        callbackDispatcher, // The top level function, aka callbackDispatcher
       // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
        );
    Workmanager.registerPeriodicTask(
      "1",
      "simpleaa",
      tag:'hi',
      initialDelay: Duration(seconds: 5),
      inputData: {'data':'data'}
    );
  print('hı');
  runApp(App());
}

class App extends StatefulWidget {
  static const APP_VERSION = 1;
  @override
  State<StatefulWidget> createState() {
    return AppState();
  }
}

class AppState extends State<App> {
  final Future _initialization = Firebase.initializeApp();

  @override
  void initState() {
    super.initState();
   

    // var timer = Timer(Duration(seconds: 30), () 
    // {
    //   justForTest();
    // });
  }

  Widget build(BuildContext context) {
    // debugPaintSizeEnabled = true;
    return FutureBuilder(
      future: _initialization,
      builder: (BuildContext context, AsyncSnapshot snap) {
        if (snap.hasData) {
          return MultiProvider(
            providers: [
              ChangeNotifierProvider<WordProvider>(
                  create: (_) => WordProvider()),
              ChangeNotifierProvider<AuthProvider>(
                  create: (_) => AuthProvider()),
              StreamProvider<UserModel>.value(
                  initialData: UserModel.fromJson({'uid': 'loading'}),
                  
                  value: AuthProvider().user)
            ],
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
              theme:ThemeData(accentColor:const  Color(0xff63CEC8) ,
              fontFamily: 'bahij',
              primaryColor: const Color(0xff202833) ,
              primaryColorLight: const Color(0xff2E3948),
              ),
                title: 'Mohammed Mehdi',
                home: Scaffold(
                  body: Tabs(),
                )),
          );
        } else {
          return Center(child: CircularProgressIndicator());
        }
      },
    );
  }

}

0 个答案:

没有答案