如何在firebase中的onvalue侦听器中获取orderbykey formate中的push键

时间:2017-10-09 06:15:07

标签: android ios firebase firebase-realtime-database flutter

我在firebasedb中有这个关键字的关键字我希望通过valueelistner enter image description here

中的价值格式来获取这些关键字

虽然我正在努力,但我得到了不同的顺序

像这样

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DemoUserDetailsService userDetailsService;

    @Autowired
    private DaoAuthenticationProvider authenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
        auth.userDetailsService(userDetailsService);
        auth.inMemoryAuthentication().withUser("user").password("password").roles("SUPER", "BASIC");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").hasRole("BASIC").and().httpBasic();
        http.csrf().disable();
    }
}

@Service
public class DemoUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        UserDAO userDAO = userRepository.findByEmailAndActivated(email);
        if (userDAO == null) {
            throw new UsernameNotFoundException(String.format("Email %s not found", email));
        }
        return new User(email, userDAO.getPasswordHash(), getGrantedAuthorities(email));
    }

    private Collection<? extends GrantedAuthority> getGrantedAuthorities(String email) {
        return asList(() -> "ROLE_BASIC");
    }
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    authenticationProvider.setUserDetailsService(userDetailsService);
    authenticationProvider.setPasswordEncoder(passwordEncoder);
    return authenticationProvider;
}

我的代码看起来像这样

  -KvhErzQOG2GE8XfzQhC 
  -KvhZRuIsl4ZbqNCrXt9
  -KvhZf3gwTsfOHsXP2M6
  -KvhLinnA_VC4KjFs_4Y: 
  -KvbPEAK39kHu98iW8Br: 
  -KvbPeRa4Bw8TEl8y5_J: 

任何人都可以为此提供帮助吗?

1 个答案:

答案 0 :(得分:0)

对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

我建议您收听onChildAdded,它会按顺序触发并且是the Flutter sample所做的:

_messagesSubscription =
    _messagesRef.limitToLast(10).onChildAdded.listen((Event event) {
  print('Child added: ${event.snapshot.value}');
});

或者你可以听onValue然后循环event.snapshot.children,但我没有一个方便的例子。