turn_bartender 0.0.2 copy "turn_bartender: ^0.0.2" to clipboard
turn_bartender: ^0.0.2 copied to clipboard

调试debug不崩溃

example/lib/main.dart

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:turn_bartender/turn_bartender.dart';
import 'package:path/path.dart' as path;

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _turnBartenderPlugin = TurnBartender();

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _turnBartenderPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  // 写入日志,注意不能并发
  void writeContent(String msg) async {
    if (!Platform.isWindows) {
      return;
    }
    String topPart = DateFormat('yyyy-MM-dd').format(DateTime.now());
    File logsFile = File("${path.dirname(Platform.resolvedExecutable)}\\${topPart}_logs.txt");
    if (!logsFile.existsSync()) {
      await logsFile.create();
    }
    logsFile.writeAsStringSync('$msg,时间:${DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now())}${Platform.lineTerminator}',mode: FileMode.append);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              ElevatedButton(
                  onPressed: () async {
                    print(await TurnBartender().getDefaultPrinterName());
                    print(await TurnBartender().getPrinterList());
                    // return;"D:/work/test.btw"
                    var res = await TurnBartender().startPrint("D:/work/test123.btw", json.encode([{"SN_KEY":"00000002469E","entityId":"A09282506030446"}]),printerName: "Microsoft Print to PDF");
                    writeContent(res.toString());
                  },
                  child: Text("打印")
              )
            ],
          ),
        ),
      ),
    );
  }
}