【问题标题】:How to parse ICalendar format in Dart?如何在 Dart 中解析 ICalendar 格式?
【发布时间】:2020-01-18 00:06:21
【问题描述】:

我需要在 Flutter 中解析 iCal 格式,但我没有找到任何包。有人可以告诉我是否存在任何解决我的问题的解决方案?

【问题讨论】:

  • 您找到解决方案了吗?我可能会在接下来的几周内完成,但显然更愿意节省一些时间。要是能做个 Flutter 插件就好了!
  • 抱歉没有解决办法:/
  • 如果你找到解决办法?
  • 还没有,但我正在利用业余时间进行实施。不过我不会屏住你的呼吸:)
  • @brindy 您是否将您的实现作为开源发布?

标签: flutter dart icalendar dart-pub


【解决方案1】:

解析/获取每个平台的日历?

快速搜索https://pub.dev/packages?q=calendar 会显示一些可能的候选人:

还有其他相关的堆栈帖子:

如果您对现有插件不满意,那么是的,不要屏住呼吸,而是通过custom platform-specific code 自己工作。

【讨论】:

    【解决方案2】:

    我不知道你是否还有问题,但我刚刚发布了一个包来解析纯 dart 中的 iCalendar 格式。

    https://pub.dev/packages/icalendar_parser

    它仍处于早期开发阶段,但可能会帮助您解决问题。

    这是一个关于如何使用该包的示例:

    import 'package:icalendar_parser/icalendar_parser.dart';
    
    // Parsing from an ICS String (ex: if you are getting data from an API)
    final iCalParsed = ICalendar.fromString(yourIcsString);
    
    // Parsing from an ICS List<String> (ex: if you are reading data from a file)
    final iCalParsed2 = ICalendar.fromLines(icsFileLines);
    

    这是我在 Flutter 中制作的完整示例:

    import 'dart:core';
    import 'dart:io';
    
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart' show rootBundle;
    import 'package:icalendar_parser/icalendar_parser.dart';
    import 'package:path_provider/path_provider.dart';
    import 'package:path/path.dart' as p;
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      ICalendar _iCalendar;
      bool _isLoading = false;
    
      Future<void> _getAssetsFile(String assetName) async {
        setState(() => _isLoading = true);
        try {
          final directory = await getTemporaryDirectory();
          final path = p.join(directory.path, assetName);
          final data = await rootBundle.load('assets/$assetName');
          final bytes =
              data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
          final file = await File(path).writeAsBytes(bytes);
          final lines = await file.readAsLines();
          setState(() {
            _iCalendar = ICalendar.fromLines(lines);
            _isLoading = false;
          });
        } catch (e) {
          setState(() => _isLoading = false);
          throw 'Error: $e';
        }
      }
    
      Widget _generateTextContent() {
        final style = const TextStyle(color: Colors.black);
        return RichText(
          text: TextSpan(
            children: [
              TextSpan(
                  text: 'VERSION: ${_iCalendar.version}\n',
                  style: style.copyWith(fontWeight: FontWeight.bold)),
              TextSpan(
                  text: 'PRODID: ${_iCalendar.prodid}\n',
                  style: style.copyWith(fontWeight: FontWeight.bold)),
              TextSpan(
                  children: _iCalendar.data
                      .map((e) => TextSpan(
                            children: e.keys
                                .map((f) => TextSpan(children: [
                                      TextSpan(
                                          text: '${f.toUpperCase()}: ',
                                          style: style.copyWith(
                                              fontWeight: FontWeight.bold)),
                                      TextSpan(text: '${e[f]}\n')
                                    ]))
                                .toList(),
                          ))
                      .toList()),
            ],
            style: style,
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                if (_isLoading || _iCalendar == null)
                  const Center(child: CircularProgressIndicator())
                else
                  _generateTextContent(),
                RaisedButton(
                  child: const Text('Load File 1'),
                  onPressed: () => _getAssetsFile('calendar.ics'),
                ),
                RaisedButton(
                  child: const Text('Load File 2'),
                  onPressed: () => _getAssetsFile('calendar2.ics'),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2021-11-12
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多