【问题标题】:Dart: PointyCastle compute asynchronousDart:PointyCastle 异步计算
【发布时间】:2020-02-19 23:45:31
【问题描述】:

目前我正在尝试将凭据数据发送到我的后端,因此出于安全目的我想对密码进行哈希处理。

但是当我使用PassCrypt().hashPass("", passwd, 48)的方法时,整个App卡顿了将近1-2秒。有没有办法异步等待输入?

谢谢:)

【问题讨论】:

  • 避免应用程序的 UI 冻结。您应该使用计算或隔离。您是否在调试模式下运行它?

标签: asynchronous flutter dart synchronous pointycastle


【解决方案1】:

您可以在下面复制粘贴运行完整代码
您可以使用compute 并使用then 获得结果
在演示中,您可以看到浮动操作按钮未被阻止

代码sn-p

Future<String> hashJob(String password) async {
      return compute(hashPassword, password);
    }

String hashPassword(String password) {
      return PassCrypt().hashPass("", password, 48);
    }

hashJob("password test").then((value) {
          hashedPassword = value;
          print(hashedPassword);
        }); 

工作演示

输出

I/flutter ( 9820): 2020-02-20 09:06:53.904373
I/flutter ( 9820): 2020-02-20 09:06:54.559547
I/flutter ( 9820): 2020-02-20 09:06:54.560054
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w
I/flutter ( 9820): J434csHIYPm0NfSJJolyuq6ykTF+x3sswIi/x9ayMAxTTE63fG63BndBCafIgN6w

完整代码

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

void main() => 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'),
    );
  }
}

Future<String> hashJob(String password) async {
  return compute(hashPassword, password);
}

String hashPassword(String password) {
  return PassCrypt().hashPass("", password, 48);
}

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;
  String hashedPassword;

  void _incrementCounter() {
    /*print(DateTime.now());
    var passHash = PassCrypt().hashPass("", "password test", 48);
    print(DateTime.now());
    print(passHash);*/

    print(DateTime.now());
    hashJob("password test").then((value) {
      hashedPassword = value;
      print(hashedPassword);
    });

    print(DateTime.now());
    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),
      ), 
    );
  }
}

【讨论】:

  • 谢谢,非常感谢,帮助了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多