패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.0.0

enhanced_future_builder

가독성을 향상시키고 일반적인 FutureBuilder 재발동 문제를 해결하는 래퍼

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
1.1천
좋아요
29
Pub 점수
140

패키지 설치

flutter pub add enhanced_future_builder

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

EnhancedFutureBuilder는 표준 FutureBuilder를 개선하여 코드 가독성을 높이고 미래가 완료될 때 발생하는 불필요한 재구성 문제를 해결하는 Flutter 패키지입니다. 비동기 데이터 처리를 위한 더 깔끔하고 직관적인 API를 제공하여 반복 코드를 줄이고 의도하지 않은 위젯 재구성을 방지합니다. Future의 확장으로 제작되어 기존 Flutter 프로젝트에 원활하게 통합할 수 있습니다. 이 패키지는 Android, iOS, Web, macOS, Windows, Linux 등 주요 플랫폼을 모두 지원합니다.

사용 사례

  • 재구성 횟수를 줄여 비동기 데이터 처리하기
  • 상태 관리에서 코드 명확성 향상
  • 미래 기반의 UI 업데이트 간소화
  • 지나치게 긴 FutureBuilder 패턴 대체
  • 로딩/에러/로딩 상태 간소화

주요 기능

  • 미래 완료 후 불필요한 재구성 방지
  • 더 깔끔하고 가독성 높은 문법
  • 로딩, 오류, 데이터 상태에 대한 내장 지원
  • 직관적인 사용을 위해 Future를 확장합니다
  • 크로스 플랫폼 호환성

적합한 대상

  • 더 간단한 비동기 UI 처리를 원하는 개발자
  • 위젯 재구성 횟수를 줄이려는 프로젝트
  • 코드 유지보수를 우선시하는 팀
  • Flutter 초보자가 비동기 패턴을 배우고 있습니다
  • 자주 비동기 데이터를 가져오는 앱

고려 사항

  • Dart 2.12 이상이 필요합니다
  • 기존의 FutureBuilder 코드를 리팩터링해야 할 수 있습니다
  • Future 기반 작업과 함께 사용 가능
  • 복잡한 스트림 시나리오에 적합하지 않습니다
  • 적절한 미래 라이프사이클 관리에 의존합니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

This library is no longer maintained. Use at your own discretion.

enhanced_future_builder

Small FutureBuilder wrapper to improve readabiltity. It can also be used as an easy solution to the common 'my FutureBuilder keeps refiring' problem (more info about that here).

Actions Status codecov

Stop FutureBuilder from refiring

Let's say you want to build an app that displays a random cat from the internet at launch and then increases a counter whenever a button is clicked. You came up with the following code:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Clicked $_counter times')
    ),
    body: FutureBuilder(
      // resolves to cat data from the internet
      future: widget._api.getRandomCat(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          // builds an image widget containing a random cat
          return _showCatWidget(snapshot.data);
        }else {
          return Center(child: Text('Loading...'));
        }
      }
    ),
    floatingActionButton: FloatingActionButton(
      // _incrementCounter calls setState() to update the view
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
}

Which results in the following app:

I love cats

As you can see there's a problem. Whenever the button is clicked, a new cat is shown to the user. This is not what we want and can be solved by using EnhancedFutureBuilder. Import the package and change the code to:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Clicked $_counter times')
    ),
    body: EnhancedFutureBuilder(
      future: widget._api.getRandomCat(),
      // this is where the magic happens
      rememberFutureResult: true,
      whenDone: (dynamic cat) => _showCatWidget(cat),
      whenNotDone: Center(child: Text('Loading...')),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
}

As you can see the code is a little easier to read now and the result will be just how we want it to be:

yeah cats are great

Usage

Ironically, EnhancedFutureBuilder doesn't require a builder anymore.

FutureBuilder:

FutureBuilder(
  future: _futureToResolve(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      return MyWidget(snapshot.data);
    }else if (snapshot.connectionState == ConnectionState.waiting){
      return Center(child: Text('Waiting...'));
    }
    }else if (snapshot.connectionState == ConnectionState.active){
      return Center(child: Text('Active...'));
    }
    // ...
  }
}

EnhancedFutureBuilder:

EnhancedFutureBuilder(
  future: _futureToResolve(),
  rememberFutureResult: false,
  whenDone: (dynamic data) => MyWidget(data),
  whenWaiting: Center(child: Text('Waiting...')),
  whenActive: Center(child: Text('Active...')),
  // whenNone: ...
),

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.