在 Flutter 中使用 BLoC 模式时,在构建应用程序代码时什么被认为是良好的编程实践?
这是一个松散的问题,所以我将尝试举一个例子。假设您定义了一个 BLoC 类来处理特定 Widget 的验证,并且您希望通过在填充表单时发出各种事件来更新验证消息。
据我了解,您的 BLoC 可能如下所示:
import 'dart:async';
import 'package:myproject/bloc/base_bloc.dart';
import 'package:rxdart/rxdart.dart';
class SignUpBloc extends Bloc {
BehaviorSubject<String> _emailSubject;
BehaviorSubject<String> _nameSubject;
BehaviorSubject<String> _phoneSubject;
BehaviorSubject<String> _signUpSubject;
SignUpBloc() {
_emailSubject = new BehaviorSubject<String>.seeded('');
_nameSubject = new BehaviorSubject<String>.seeded('');
_phoneSubject = new BehaviorSubject<String>.seeded('');
_signUpSubject = new BehaviorSubject<String>.seeded('');
}
void nameChanged(String content) {
if (content?.isEmpty ?? true) {
_nameSubject.emit('Name is required for the sign-up process');
} else {
_nameSubject.emit('');
}
}
void emailChanged(String content) {
if (!_validEmail(content)) { …Run Code Online (Sandbox Code Playgroud) 现在我想首先说我已经检查了所有关于这个主题的非常相似的文章,到目前为止还没有解决我的问题.
我正在尝试设置.NET Core的Entity Framework,当我尝试访问'UseSqlServer'方法时,我一直收到错误.根据我读过的其他文章,这实际上是一个在Microsoft.EntityFrameworkCore中定义的扩展方法...我已经手动添加了对此的引用,并且可以确认它没有解决我的问题.
受影响的类非常简单:
using ActivityService.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
namespace ActivityService
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// …Run Code Online (Sandbox Code Playgroud)