AndroidAlarmManager无效的日期漂移

问题描述 投票:0回答:1

我在使用AndroidAlarmManager时遇到问题。我想设置警报,但看到错误:

Invalid date format
2020-03-09 9:45:00

仅当我取消注释行时,才会出现此错误:

  AndroidAlarmManager.oneShotAt(l2p, Random().nextInt(9999999), () =>start());

我尝试使用DateTime.now()代替“ 12p”变量,但效果仍然相同。代码:

String formattedDate = DateFormat('kk:mm:ss').format(DateTime.now());
  String date = DateFormat('yyyy-MM-dd').format(DateTime.now());
  var l2 = DateTime.parse('$date 08:50:00');
  var l3 = DateTime.parse('$date 09:45:00');
  var l4 = DateTime.parse('$date 10:40:00');
  var l5 = DateTime.parse('$date 11:40:00');
  var l6 = DateTime.parse('$date 12:40:00');
  var l7 = DateTime.parse('$date 13:40:00');
  var l8 = DateTime.parse('$date 14:35:00');
  var l2p = DateTime.parse('$date 08:45:00');
  var l3p = DateTime.parse('$date 9:45:00');
  var l4p = DateTime.parse('$date 10:40:00');
  var l5p = DateTime.parse('$date 11:40:00');
  var l6p = DateTime.parse('$date 12:40:00');
  var l7p = DateTime.parse('$date 13:40:00');
  var l8p = DateTime.parse('$date 14:35:00');


  AndroidAlarmManager.oneShotAt(l2p, Random().nextInt(9999999), () =>start());

  AndroidAlarmManager.oneShotAt(l2, Random().nextInt(9999999), () =>stop());

  AndroidAlarmManager.oneShotAt(l3p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l3, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l4p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l4, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l5p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l5, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l6p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l6, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l7p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l7, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l8p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l8, Random().nextInt(9999999), () =>stop());
flutter dart alarmmanager android-alarms
1个回答
0
投票

您输了一个0请从

更改
  var l3p = DateTime.parse('$date 9:45:00');

收件人

  var l3p = DateTime.parse('$date 09:45:00');

完整的测试代码

import 'package:flutter/material.dart';
import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:intl/intl.dart';
import 'dart:math';

start () {
  print("start");
}

stop() {
  print("stop");
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  String formattedDate = DateFormat('kk:mm:ss').format(DateTime.now());
  String date = DateFormat('yyyy-MM-dd').format(DateTime.now());
  var l2 = DateTime.parse('$date 08:50:00');
  var l3 = DateTime.parse('$date 09:45:00');
  var l4 = DateTime.parse('$date 10:40:00');
  var l5 = DateTime.parse('$date 11:40:00');
  var l6 = DateTime.parse('$date 12:40:00');
  var l7 = DateTime.parse('$date 13:40:00');
  var l8 = DateTime.parse('$date 14:35:00');
  var l2p = DateTime.parse('$date 08:45:00');
  var l3p = DateTime.parse('$date 09:45:00');
  var l4p = DateTime.parse('$date 10:40:00');
  var l5p = DateTime.parse('$date 11:40:00');
  var l6p = DateTime.parse('$date 12:40:00');
  var l7p = DateTime.parse('$date 13:40:00');
  var l8p = DateTime.parse('$date 14:35:00');


  AndroidAlarmManager.oneShotAt(l2p, Random().nextInt(9999999), () =>start());

  AndroidAlarmManager.oneShotAt(l2, Random().nextInt(9999999), () =>stop());

  AndroidAlarmManager.oneShotAt(l3p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l3, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l4p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l4, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l5p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l5, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l6p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l6, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l7p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l7, Random().nextInt(9999999), () =>stop());
  AndroidAlarmManager.oneShotAt(l8p, Random().nextInt(9999999), () =>start());
  AndroidAlarmManager.oneShotAt(l8, Random().nextInt(9999999), () =>stop());

  runApp(MyApp()); }

class MyApp extends StatelessWidget { 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(      
        primarySwatch: Colors.blue,
      ),
      home: 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> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {     
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {   
    return Scaffold(
      appBar: AppBar(        
        title: Text(widget.title),
      ),
      body: Center(       
        child: Column(          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.