Asynchronous Dart:PointyCastle异步计算

Asynchronous Dart:PointyCastle异步计算,asynchronous,flutter,dart,synchronous,pointycastle,Asynchronous,Flutter,Dart,Synchronous,Pointycastle,目前,我正在尝试将凭据数据发送到后端,因此出于安全目的,我希望散列密码 但是当我使用方法PassCrypt().hashPass(“,passwd,48)时,整个应用程序冻结了将近1-2秒。有没有办法异步等待输入 谢谢:)您可以复制粘贴运行下面的完整代码 您可以使用计算并使用获得结果,然后 在演示中,您可以看到浮动操作按钮未被阻止 代码片段 Future<String> hashJob(String password) async { return compute(has

目前,我正在尝试将凭据数据发送到后端,因此出于安全目的,我希望散列密码

但是当我使用方法
PassCrypt().hashPass(“,passwd,48)
时,整个应用程序冻结了将近1-2秒。有没有办法异步等待输入


谢谢:)

您可以复制粘贴运行下面的完整代码
您可以使用
计算
并使用
获得结果,然后

在演示中,您可以看到浮动操作按钮未被阻止

代码片段

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);
        }); 
完整代码

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),
      ), 
    );
  }
}
导入“包装:颤振/材料.省道”;
进口“包装:钢穴/钢穴.省道”;
导入“dart:async”;
进口“包装:颤振/基础.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题资料(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
未来hashJob(字符串密码)异步{
返回compute(hashPassword,password);
}
字符串hashPassword(字符串密码){
返回PassCrypt().hashPass(“,password,48);
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
字符串哈希密码;
void _incrementCounter(){
/*打印(DateTime.now());
var passHash=PassCrypt().hashPass(“,“密码测试”,48);
打印(DateTime.now());
打印(passHash)*/
打印(DateTime.now());
hashJob(“密码测试”)。然后((值){
hashedPassword=值;
打印(哈希密码);
});
打印(DateTime.now());
设置状态((){
_计数器++;
});
}
@凌驾
小部件生成(BuildContext上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中心(
子栏(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
), 
);
}
}

以避免应用程序的UI冻结。您应该使用compute或isolate。是否在调试模式下运行它?谢谢,非常感谢,帮助:)
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),
      ), 
    );
  }
}